microsoft / microsoft/TypeScript

Preserve property comment and insert @default value, if provided

Open
#41,486 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named in the issue. Start by tracing how TypeScript builds TSDoc ASTs and hover information for destructured parameters, then verify how an initializer can preserve the original property comment and add a @default value. Done means the shown host example retains its comment and exposes the default value without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.