microsoft / microsoft/TypeScript
Unable to invoke an arrow function with an assertion predicate
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔎 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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con la reproducción en TypeScript Playground y compara el tratamiento de los predicados de aserción de las funciones flecha con el comportamiento descrito en los PRs #45952 y #32695. Rastrea el requisito de tipo explícito para la función flecha vinculada a const y, a continuación, valida el comportamiento esperado con una prueba del compilador centrada en el ejemplo proporcionado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100