microsoft / microsoft/TypeScript
An alternative option to `keyofStringsOnly` that stringifies numeric properties and index signatures
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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;
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die verknüpften Issues und Pull Requests sowie die Beispiele für keyof, numerische Eigenschaften und Indexsignaturen in diesem Issue zu prüfen. Ermittle die Design-Auswirkungen der Umwandlung numerischer Schlüssel in Zeichenfolgen und die angegebene Abhängigkeit von pattern literal index signatures. Als erledigt gilt die Aufgabe, wenn die vorgeschlagene Option die angeforderten keyof-Ergebnisse liefert, ohne das bestehende Verhalten zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100