microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.86k stars 12.36k forks source link

JSDoc export type support #48104

Open xiaoxiangmoe opened 2 years ago

xiaoxiangmoe commented 2 years ago

Suggestion

🔍 Search Terms

JSDoc

✅ Viability Checklist

My suggestion meets these guidelines:

⭐ 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.

hildjj commented 1 year ago

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 commented 1 year ago

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

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 commented 1 month ago

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}