microsoft / microsoft/TypeScript
An alternative option to `keyofStringsOnly` that stringifies numeric properties and index signatures
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked issues and pull requests, along with the examples for keyof, numeric properties, and index signatures in this issue. Determine the design implications of stringifying numeric keys and the stated dependency on pattern literal index signatures. Done means the proposed option produces the requested keyof results without changing existing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100