microsoft / microsoft/TypeScript

Optional arguments in JSDoc types may be moved to TypeScript types quickfix

Open
#31,497 2 comments 1 reaction 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

TypeScript Version: 3.4.3

  • 80004
  • jsdoc quickfix optional

Code

With Closure Compiler, you mark a function parameter optional by adding an = after its type (reference):

/**
 * @param a {string} Base string.
 * @param b {string=} Optional string to add
 * @return {string}
 */
function foo(a, b) {
  if (b) return a + b;
  return a;
}

TypeScript offers a quickfix to convert these jsdoc annotations to TypeScript types:

image

Expected behavior:

The b parameter should be optional:

/**
 * @param a {string} Base string.
 * @param b {string=} Optional string to add
 * @return {string}
 */
function foo(a: string, b?: string): string {
  if (b) return a + b;
  return a;
}

Actual behavior:

The b parameter is required but has type string | optional:

/**
 * @param a {string} Base string.
 * @param b {string=} Optional string to add
 * @return {string}
 */
function foo(a: string, b: string | undefined): string {
  if (b) return a + b;
  return a;
}

This means that all calls of the function which don't specify b will be flagged as errors.

Playground Link: link, though quickfixes are not available there

Related Issues:

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

Start by reproducing the JSDoc-to-TypeScript types quickfix with the provided foo function and compare the generated parameter type with the expected optional parameter. Trace the quickfix entry point and add regression coverage showing that calls omitting b are accepted while the generated return type remains string.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.