Skip to content

Add JSDoc @export tagย #60831

Open
Open
@remcohaszing

Description

@remcohaszing

๐Ÿ” Search Terms

jsdoc @export

โœ… Viability Checklist

โญ Suggestion

TypeScript 5.5 added the @import JSDoc tag which can be used for type-only imports. It would be useful to add an analogous @export tag for type-only exports.

๐Ÿ“ƒ Motivating Example

Letโ€™s say you maintain a library that contains the following file:

// types.js
/**
 * @typedef {string} Username
 */

/**
 * @typedef {number} UserId
 */

Maybe you want to expose these types in the main entry point. You can now do this with the JSDoc @export tag. It works the same as type-only exports in TypeScript files.

// index.js
/**
 * @import { Username } from './types.js'
 */

/**
 * @export { Username }
 * @export { UserId } from './types.js'
 */

๐Ÿ’ป Use Cases

This is useful to have full control over exports in a library. Also this is just parity between JSDoc based types and TypeScript files.

I can think of 3 workarounds, all of which are not great.

  1. Define a new type in the index file using @typedef

    // index.js
    /**
     * @typedef {import('./types.js').Username} Username
     * @typedef {import('./types.js').UserId} UserId
     */

    Pros:

    • It works

    Cons:

  2. In addition to index.js, create a TypeScript file, e.g. exports.ts. Then in exports.ts write something along the lines of:

    export { someValue, anotherValue } from './index.js'
    export { Username, UserId } from './types.js'

    and in package.json, write something like:

    {
      "types": "./types/exports.d.ts",
      "exports": {
        "types": "./types/exports.d.ts",
        "default": "./lib/index.js",
      }
    }

    Pros:

    • It works
    • No new types are defined, but actual exports
    • Hover information is preserved
    • Type parameters are preserved

    Cons:

    • Exports from index.js need to be synced to exports.ts manually.
    • Youโ€™re essentially tricking TypeScript into thinking exports.ts is the main entry, whereas in reality itโ€™s index.js
  3. Manually write the declaration file.

    Pros:

    • It works
    • No new types are defined

    Cons:

    • You need to author the declaration files manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: JSDocRelates to JSDoc parsing and type generationSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions