microsoft / microsoft/TypeScript

generate function declaration from function type in TypeScript

Offen
#22,395 1 Kommentar 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: LS: Quick Fixes Experience Enhancement In Discussion Suggestion VS Code Tracked
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

From @unional on October 5, 2017 18:52

type MyCallback = (a: number, b: string) => void

function foo(callback: MyCallback) {
  // ...
}

// case 1
foo((a, b) => { ... })

// case 2
foo(function (a, b) { ... })

// case 3
function cb(a: number, b: string) { ... }
foo(cb)

Above are 3 common ways to write a callback.
TypeScript is great at inferring the argument types in the implemented callbacks.
a, and b are typed accordingly in the callbacks above.

However, the tooltip is not useful when we are implementing the callbacks:

image

If I change foo() to not using type alias, it helps in case 1 and 2:

function foo(callback: (a: number, b: string) => void) { ... }

image

But that doesn't help in case 3 and actually demotes type alias.

One solution I can think of is using [ctrl+.] to fill in the callback:

foo(/* caret */)

// [ctrl+.] and choose "implement callback as arrow function":
foo((a: number, b: string) => { /* caret */ })

// [ctrl+.] and choose "implement callback as function declaration":
foo(function (a: number, b: string) { /* caret */ })

// caret anywhere on the type alias below
MyCallback

// [ctrl+.] and choose "implement function declaration" will expand to snippet:
function ${1:functionName}(a: number, b: string): void { ${0} }

Copied from original issue: Microsoft/vscode#35635

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

Das Issue nennt keine Implementierungsdateien oder Tests. Beginne damit, den ctrl+. code-action-Einstiegspunkt für die Generierung von callback und function-declaration nachzuverfolgen, und verifiziere anschließend, dass die angeforderten Aktionen in allen drei Beispielen inferierte callback-Parameter und type aliases unterstützen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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