Skip to content

Preserve property comment and insert @default value, if provided #41486

Open
@jednano

Description

@jednano

Search Terms

default initial initializer value preserve comment tsdoc type options option bag

Suggestion

As the title suggests, insert a @default comment to the TSDoc AST if an initializer is provided, preserving the original comment.

Use Cases

I have a default value for an option and I want the consumer of the library to know what the default value is.

Examples

interface Options {
  /**
   * The server host.
   */
  host?: string;
  port?: number;
}

function serve({ host = 'localhost', port }: Options) {
  return { host, port };
}

Playground

Hovering over host in the return statement shows the following in vscode:

Screen Shot 2020-11-10 at 6 39 31 PM

Not only is the TSDoc comment lost for this property, but it could furthermore be improved if @default "localhost" were added to it, seeing as that information is available in this context.

It should look like this:

Screen Shot 2020-11-10 at 6 49 35 PM

Much ❤️ for the TS team!

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions