Skip to content

JSDoc export type support #48104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
xiaoxiangmoe opened this issue Mar 3, 2022 · 3 comments
Open
5 tasks done

JSDoc export type support #48104

xiaoxiangmoe opened this issue Mar 3, 2022 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@xiaoxiangmoe
Copy link
Contributor

xiaoxiangmoe commented Mar 3, 2022

Suggestion

πŸ” Search Terms

JSDoc

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

πŸ“ƒ Motivating Example

/**
 * @typedef { { loading: boolean, data: any } } AjaxStatus  
 */

Can we export AjaxStatus to other files?

such as

/**
 * @export type { AjaxStatus, AjaxStatus as AjaxStatus2 , AjaxStatus as default }
 */

πŸ’» Use Cases

Used for JavaScript development using the TypeScript language service.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Mar 4, 2022
@hildjj
Copy link

hildjj commented May 30, 2023

Use case:

class Foo {
  /**
   * @typedef {object} FooOptions
   * @prop {boolean} [verbose]
   */
  /**
   * @param {FooOptions} opts
   */
  constructor(opts = {}) {
    /** @type {Required<FooOptions>} */
    this.opts = {
      verbose: false,
      ...opts,
    };
  }
}

In the project containing Foo, typedoc complains that FooOptions isn't exported, and can't generate any documentation other than the type name.

Next, I'd like to subclass Foo in another project. I'd like to pass a FooOptions into that subclass. I can get there with ConstructorParameters<typeof Foo>[0], but that's pretty difficult to document. I suppose I could also copy in the definition of FooOptions, but then I'm just back to the first problem.

@hildjj
Copy link

hildjj commented May 30, 2023

A suggestion for an approach that might work, different from the one suggested in the original issue might be:

  • A JSDoc tag @export
  • Does not conflict with the JSDoc @exports tag, which is mostly useless currently
  • Matches the Typescript and JS keyword export
  • Cannot do export default (I don't feel strongly about this, but it's not worth adding more syntax, in my opinion)
  • Only applies to types, not things that you can export using JS syntax
  • As such, applies only to @typedef, @callback, and perhaps some @template blocks
  • Causes export type to be generated into the .d.ts file
  • Uses the type alias in the generated .d.ts file, instead of expanding the type before generation.
  • @export is not required if the type is otherwise exposed. For example, if it is used as a parameter to an exported function or method.

For the example above, instead of generating this:

export class Foo {
    constructor(opts?: {
        verbose?: boolean | undefined;
    });
    opts: Required<{
        verbose?: boolean | undefined;
    }>;
}

You would get this:

export interface FooOptions {
  verbose?: boolean | undefined;
}
export class Foo {
    constructor(opts?: FooOptions);
    opts: Required<FooOptions>;
}

@nmss
Copy link

nmss commented Jul 12, 2024

Now that typescript 5.5 have brought the @import syntax, it would be nice to have the @export syntax

also it does not seem possible to use @typedef to re-export a type imported with @import

use case exemple

/** @import {MyType} from 'my-module' */
/** @export MyType */

example corresponding typescript syntax

import type {MyType} from 'my-module'
export type {MyType}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants