microsoft / microsoft/TypeScript

Feature request: Override and extend callback argument

Offen
#58,788 0 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.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

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 Quelldateien, Tests oder Einstiegspunkte. Beginne damit, die vorgeschlagenen Syntaxformen für Callback-Parameter und as extends zu vergleichen und die Designdiskussion zu prüfen; die Arbeit ist erst abgeschlossen, wenn ein abgegrenzter Vorschlag vereinbart wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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