microsoft / microsoft/TypeScript
An alternative option to `keyofStringsOnly` that stringifies numeric properties and index signatures
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
Suggestion
🔍 Search Terms
- keyof
- keyofStringsOnly
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Since pattern literal types like `${number}` are valid as of https://github.com/microsoft/TypeScript/pull/40598, I feel like there should be an option to make:
type ArrayLikeKeys = keyof ArrayLike<any>;
result in:
type ArrayLikeKeys = "length" | `${number}`;
The same should happen for any numeric property key:
interface Foo {
1: "a";
2: "b";
3: "c";
}
// Currently is: : 1 | 2 | 3
// Should be: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;
// Currently is: never
type StrictKeyOfFoo = (keyof Foo) & (string | symbol);
like with:
interface Foo {
"1": "a";
"2": "b";
"3": "c";
}
// Is: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;
📃 Motivating Example
This makes keyof and numeric index signatures match runtime behaviour.
💻 Use Cases
Currently, it’s necessary to use the strictKeyof and StrictPropertyKey helpers:
type strictKeyof<T> = keyof T extends infer K
? (K extends number ? `${K}` : K)
: never;
type StrictPropertyKey = string | symbol;
Relevant issues:
- https://github.com/microsoft/TypeScript/issues/27995
- https://github.com/microsoft/TypeScript/pull/35594#discussion_r355727393, https://github.com/microsoft/TypeScript/pull/35594#discussion_r356496551, https://github.com/microsoft/TypeScript/pull/35594#discussion_r355728724
- https://github.com/microsoft/TypeScript/issues/41669
- https://github.com/microsoft/TypeScript/pull/41987
- https://github.com/microsoft/TypeScript/pull/48837
- https://github.com/microsoft/TypeScript/issues/48957
This will most likely depend on https://github.com/microsoft/TypeScript/pull/26797, so that treating numeric index signatures as numeric pattern literal index signatures is valid:
interface ArrayLike<T> {
readonly [index: `${number}`]: T;
readonly length: number;
}
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 revisando los issues y pull requests enlazados, junto con los ejemplos de keyof, propiedades numéricas y firmas de índice de este issue. Determina las implicaciones de diseño de convertir las claves numéricas en cadenas y la dependencia indicada de las pattern literal index signatures. Se considera terminado cuando la opción propuesta produce los resultados de keyof solicitados sin cambiar el comportamiento existente.
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
- Bastante claro
- Aptitud para principiantes
- 30/100