microsoft / microsoft/TypeScript
Destructuring object with generic key should reject undefined
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
🔎 Search Terms
destructure, generic key, undefined
🕗 Version & Regression Information
This is the behavior in every version I tried (back to at least 5.0), and I reviewed the FAQ for entries about destructuring
⏯ Playground Link
💻 Code
interface T { a?: unknown; b?: unknown; c?: unknown }
function f<L extends keyof T>(k: L, t: T | undefined): unknown {
const { [k]: v } = t // should error (crashes if t undefined)
const _v2 = t[k] // equivalent code, does error
return v
}
🙁 Actual behavior
TS does not error on the destructuring, even though it will crash at runtime if t is undefined, and it errors on equivalent code using object-access syntax.
🙂 Expected behavior
TS should error on the indicated line.
Additional information about the issue
This is simplified from an actual example in our codebase. Obviously in this case it's silly to destructure but this was part of a larger block of destructuring so the code would at a glance seem reasonable.
Note that the destructured value does end up having a funky type (T | undefined)[L], which presumably it's hard to do anything with. But of course it's still assignable to unknown, and thus usable in various ways. (In the actual code in question, the authors were doing other complicated things, running an issue similar to #40312, and ended up casting through unknown to the type they wanted.)
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 ejemplo de Playground enlazado y compara el manejo que hace el checker de la desestructuración de propiedades calculadas con la expresión equivalente de acceso indexado. Sigue la clave genérica y el objeto posiblemente indefinido durante la comprobación de tipos; se considera terminado cuando la línea de desestructuración recibe un error adecuado sin provocar regresiones en la desestructuración válida.
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
- Bien especificado
- Aptitud para principiantes
- 38/100