microsoft / microsoft/TypeScript
Type Guard Issue with Array.prototype.fill, Array Constructor
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
Typegurd Issue with Array.prototype.fill, Array Constructor
TypeScript Version: 3.5.1
Search Terms:
- Array.prototype.fill
- fill
- ArrayConstructor
- array implicit any
Code
const foo: number[] = new Array(3).fill("foo"); // accepted
Actual behavior:
This code above is accepted because new Array(3) returns any[], and so fill("foo") returns any[].
I know giving type explicitly to Array like
const foo: number[] = new Array<number>(3).fill("foo"); // error
would work, but I believe the compiler should reject the first one. (This is TypeScript.)
Expected behavior:
Option A. Array.prototype.fill returns narrow type
replace declaration of Array.fill in lib.es2015.core.d.ts like fill<S extends T>(value: S, start?: number, end?: number): S[];, then
const foo: number[] = new Array(3).fill("foo");
// error: "Type 'string[]' is not assignable to type 'number[]'."
// because `fill` is resolved as `fill<string>(value: string, .....): string[]`
const bar = new Array(3).fill(0); // `bar` is resolved as `number[]`
bar.fill("bar"); // error: "Type '"bar"' is not assignable to type 'number'."
const baz: (number|string)[] = new Array(3).fill(0); // accepted.
baz.fill("baz"); // accepted.
Option B. Array Constructor never return implicit any
The problem is that Array constructor returns any[] implicitly. The first code is accepted even with --strict or any other options. It means we always have to care about "Array constructor returns any[]".
Something like #26188 could solve this issue.
Playground Link:
https://www.typescriptlang.org/play/#src=const%20foo%3A%20number%5B%5D%20%3D%20new%20Array(3).fill(%22foo%22)%3B
Related Issues:
#29604
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 reproduciendo el ejemplo en el TypeScript Playground enlazado y, después, lee las declaraciones de Array y ArrayConstructor en lib.es2015.core.d.ts. Revisa los issues relacionados #29604 y #26188 y determina cuál de los comportamientos propuestos es el previsto. La tarea está terminada cuando el comportamiento aceptado está especificado y cubierto por las pruebas relevantes de TypeScript.
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