microsoft / microsoft/TypeScript
Unable to invoke an arrow function with an assertion predicate
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
arrow function, assertion, predicate, 2775
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione in TypeScript Playground e confronta la gestione dei predicati di asserzione delle funzioni freccia con il comportamento descritto nelle PR #45952 e #32695. Traccia il requisito di un tipo esplicito per la funzione freccia associata a const, quindi valida il comportamento previsto con un test mirato del compilatore che copra l’esempio fornito.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100