microsoft / microsoft/TypeScript
Possible to narrow type of object literal property with strictNullChecks?
Abierto
@sandersn ya está trabajando en esto.
Desde el 30/1/2018.
Committed
Suggestion
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
TypeScript Version: Current playground (2.6?)
Code
type Foo = {
bar?: number[];
}
let f1: Foo = {};
f1.bar = [];
f1.bar.push(0);
let f2: Foo = { bar: [] };
f2.bar.push(0);
Expected behavior:
Both references to .bar compile successfully.
Actual behavior:
Second reference (f2.bar.push(0)) fails with "object is possibly undefined" -- the type of f2.bar is still treated as number[] | undefined even though it obviously can't be undefined.
This might not be an actual bug as such but whatever magic allows the compiler to narrow the type of f1.bar after assigning to it, should also be able to narrow the type when it was assigned in the literal.
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.
Evaluación
Este issue todavía no se ha evaluado.