microsoft / microsoft/TypeScript
Index signatures in type definitions: now we have `noUncheckedIndexedAccess`, include or omit `undefined`?
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
lib Update Request
Before we had noUncheckedIndexedAccess, it was common practice to add undefined to an index signature as a workaround. For example:
- https://github.com/microsoft/TypeScript/issues/13792 in which the index signature inside
DOMStringMap(lib.dom.d.ts) was modified to includeundefined. - https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43931 in which all index signatures in
@types/nodewere modified to includeundefined(e.g.ProcessEnvandParsedUrlQuery).
However, it seems this workaround wasn't extended to all index signatures, e.g.
RegExpMatchArray['groups'] in lib.es2018.regexp.d.ts and PropertyDescriptorMap in lib.es5.d.ts.
Now we have noUncheckedIndexedAccess I would like to ask what the best way forward is regarding these workarounds? For people who enable noUncheckedIndexedAccess, these workarounds are redundant. For people who don't enable noUncheckedIndexedAccess, these workarounds are still necessary—but potentially not always desirable since it forces a higher level of strictness.
It seems inconsistent that some index signatures use this workaround and others do not, so I think we should decide either to use this workaround in all index signatures or remove it from all index signatures and suggest people use noUncheckedIndexedAccess.
Including undefined creates a few problems, for example:
declare const v: DOMStringMap;
Object.values(v).map((value) => {
// Expected: string;
// Actual: string | undefined; ❌
value;
});
for (const value of Object.values(v)) {
// Expected: string;
// Actual: string | undefined; ❌
value;
}
const test: DOMStringMap = {
// This should error
foo: undefined
}
If the TypeScript team can decide the best way forward then this will also provide some guidance to the community—for example we're trying to decide what the best option is for @types/node: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/51177.
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 comparando las firmas de índice en lib.es2018.regexp.d.ts, lib.es5.d.ts y lib.dom.d.ts, y después revisa los ejemplos del issue relacionados con noUncheckedIndexedAccess. El issue no especifica qué política adoptar, por lo que sería necesario que el equipo tomara una decisión antes de actualizar las definiciones afectadas y comprobar su impacto en el comportamiento citado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100