microsoft / microsoft/TypeScript

Feature request: Override and extend callback argument

Aperta
#58,788 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

extend callback argument typescript

✅ Viability Checklist
⭐ Suggestion

(Possibly a duplicate, but I couldn't find similar issue)
I often need to change type of a callback argument and it always takes an extra line to perform as assertions. I propose to add ability to use as keyword directly on the argument.

function withCallback(callback: (arg: { foo: string }) => void) {
  // ...
}

withCallback((arg) => {
  const { foo, bar } = arg as { foo: string; bar: string }; // always an extra line
  // ...
});
📃 Motivating Example

It would be nice to have a built-in TS feature that makes possible to do that in a shorter manner:

withCallback(({ foo, bar } as { foo: string; bar: string }) => {
  console.log(foo, bar);
});

// or
withCallback(({ foo, bar } as unknown as { foo: string; bar: string }) => {
  console.log(foo, bar);
});

Overriding the type completely isn't needed too often that's why I propose to also enrich this syntax with extends (or something more applicable) keyword:

withCallback(({ foo, bar } as extends { bar: string }) => {
  console.log(foo, bar);
});

Thinking thru it I think it would make sense to also add as extends for other often appearing use-cases:

const x = { foo: 'foo' };

const y = x as extends { bar: string };
// same as 
const y = x as typeof x & { bar: string };

as extends T at this syntax can be interpreted as as typeof __SELF__ & T

💻 Use Cases

There are million use-cases for this feature. Type assertions in TS are used more often than we might expect while learning it.

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

La issue non indica file sorgente, test o punti di ingresso. Inizia confrontando le forme di sintassi proposte per i parametri di callback e as extends e rivedendo la discussione sul design; il lavoro sarà completo solo dopo aver concordato una proposta circoscritta.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.