microsoft / microsoft/TypeScript
TS 3.7: unlike `x is T`, `asserts x is T` cannot close over generics defined in outer scopes
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
TypeScript Version: 3.7.0-dev.20191016
Search Terms:
asserts, asserts return, asserts higher order, asserts type, 2775
Code
function literal<T extends keyof any>(lit: T): {
is(value: any): value is T
assert(value: any): asserts value is T
} {
return null as any; // implementation doesn't matter
}
const isHi = literal("hi")
const x: unknown = "test"
if (isHi.is(x)) {
console.log(x) // x is correctly inferred to be 'hi' :)
}
isHi.assert(x); // error: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
console.log(x); // x should be inferred to be 'hi' here :(
Expected behavior:
No compile error, x is inferred to be "hi" on the last line.
Actual behavior:
Compile error on isHi.assert. Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
Construction higher order type guards is possible without problem (as shown in the snippet). This mechanism used heavily in libraries like io-ts and mobx-state-tree.
However, when trying to extend the latter library with assertion functionality for more convenient control flow, we run into this issue.
We can build type.is properly, but not type.assert, although they seem to be needing the exact same type / depth of type analysis; if type guards can close over T, so should type assertions?
Playground Link: link
Related Issues:
#34523
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 vinculada en TypeScript Playground y revisa el issue #34523 relacionado, confirmando diagnostic 2775 para el ejemplo de aserción de orden superior. El trabajo estará terminado cuando el ejemplo compile sin ese error y el uso final de x se infiera como "hi".
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100