microsoft / microsoft/TypeScript
nested destructured tuple value is `string | undefined` when second tuple item is not accessed
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Bug Report
When using noUncheckedIndexedAccess, the type inferred by my code is affected by whether I access the second item in a tuple or not.
🔎 Search Terms
tuple, undefined, type, omit, noUncheckedIndexedAccess, error, index, array, infer, nested
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
noUncheckedIndexedAccess - I was unable to test this on prior versions because
--noUncheckedIndexedAccessdidn't exist before those versions
⏯ Playground Link
Playground link with relevant code
💻 Code
declare const fn: <T extends string[]>(t: T) => [T, string[]];
const [[x]] = fn(['1']);
console.log(x.toLowerCase());
It seems that the inferred type for fn is:
const fn: <string[]>(t: string[]) => [string[], string[]]
Since the first item is an array rather than a tuple, items in that array have type string | undefined.
If I change the assignment to be:
const [[x], []] = fn(['1']);
It gets inferred as:
const fn: <[string]>(t: [string]) => [[string], string[]]
Since the first item is a tuple rather than an array, items in that tuple have type string.
🙁 Actual behavior
x has type string | undefined
🙂 Expected behavior
x has type string.
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 el TypeScript Playground enlazado y reproduce las dos formas de desestructuración con noUncheckedIndexedAccess. Rastrea los tipos inferidos diferentes que se muestran en el informe; la tarea estará terminada cuando la primera forma dé a x el tipo string y conserve el comportamiento esperado para la segunda forma.
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