microsoft / microsoft/TypeScript

generate function declaration from function type in TypeScript

Aperta
#22,395 1 commento 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: LS: Quick Fixes Experience Enhancement In Discussion Suggestion VS Code Tracked
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

L’issue non indica file di implementazione o test. Inizia tracciando l’entry point ctrl+. di code-action per la generazione di callback e function-declaration, quindi verifica che le azioni richieste supportino parametri di callback inferiti e type aliases in tutti e tre gli esempi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.