microsoft / microsoft/TypeScript

Feature request: Override and extend callback argument

Ouverte
#58,788 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne nomme aucun fichier source, test ou point d’entrée. Commencez par comparer les formes de syntaxe proposées pour les paramètres de callback et as extends, et par examiner la discussion de conception ; le travail ne sera terminé qu’une fois qu’une proposition limitée aura été convenue.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.