microsoft / microsoft/TypeScript

Unable to invoke an arrow function with an assertion predicate

Ouverte
#56,147 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

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

Description

🔎 Search Terms

arrow function, assertion, predicate, 2775

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://tsplay.dev/w8QLpN

💻 Code
type A = { x: number };

type B = A & { y: number };

const foo = (a: A): asserts a is B => {
  if ((a as B).y !== 0) throw TypeError();
  return undefined;
};

export const main = () => {
  const a: A = { x: 1 };
  // TypeError: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
  foo(a);
};
🙁 Actual behavior

It looks like they goal of https://github.com/microsoft/TypeScript/pull/45952 was for assertion predicates to work for arrow functions, but this only handles the JSDoc case, which I assume TS treats as equivalent to directly annotating the entire function at once, i.e.:

 const foo: (a: A) => asserts a is B = (a) => {};

For functions with more named parameters, this annotation style quickly becomes unwieldy. There is no semantic difference between this annotation or the style from the repro:

const foo = (a: A): asserts a is B => {};

Is it possible to identify a top-level arrow function like this assigned to a const variable with annotated parameters and return as having an "explicit type," which was defined as the standard for being able to use an assertion predicate like this in @ahejlsberg's initial PR (https://github.com/microsoft/TypeScript/pull/32695)?

This style is increasingly common, and I think many developers (my self included) simply write a function like this, see it doesn't work, then choose not to use the feature, not knowing something like moving the annotation to the variable itself could have an effect.

🙂 Expected behavior

(see above)

Additional information about the issue

No response

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

Commencez par la reproduction dans TypeScript Playground et comparez la gestion des prédicats d’assertion des fonctions fléchées avec le comportement décrit dans les PR #45952 et #32695. Suivez l’exigence de type explicite pour la fonction fléchée liée à const, puis validez le comportement attendu avec un test ciblé du compilateur couvrant l’exemple fourni.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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