microsoft / microsoft/TypeScript

Unable to invoke an arrow function with an assertion predicate

Offen
#56,147 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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

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

Beginne mit der Reproduktion im TypeScript Playground und vergleiche die Behandlung von Assertion-Prädikaten in Pfeilfunktionen mit dem in den PRs #45952 und #32695 beschriebenen Verhalten. Verfolge die Anforderung eines expliziten Typs für die an const gebundene Pfeilfunktion und validiere anschließend das erwartete Verhalten mit einem fokussierten Compiler-Test, der das bereitgestellte Beispiel abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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