microsoft / microsoft/TypeScript

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

Offen
#31,497 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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:

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie damit, den Quickfix für JSDoc-to-TypeScript-Typen mit der bereitgestellten foo-Funktion zu reproduzieren und vergleichen Sie den generierten Parametertyp mit dem erwarteten optionalen Parameter. Verfolgen Sie den Einstiegspunkt des Quickfixes und fügen Sie eine Regressionstestabdeckung hinzu, die zeigt, dass Aufrufe, bei denen b weggelassen wird, akzeptiert werden, während der generierte Rückgabetyp string bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers, tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.