Closed
Description
Suggestion
π Search Terms
object key dotted path template literal helper type
β 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
I'd like to see a built-in helper type that takes an interface as a generic param, and returns either the union of all dotted-notation key paths, or tuples of valid paths:
interface Foo {
a: {
b: {
c: number;
}[];
d: boolean;
}
}
type FooPath = ObjectKeyPath<Foo>; // "a" | "a.b" | `a.${number}.c` | "a.d";
type FooTuple = ObjectKeyTuple<Foo>; // ["a"] | ["a", "b"] | ["a", number, "c"] | ["a", "d"];
π Motivating Example
Both of these notations are commonly used in libraries around the ecosystem. Many end users try to create similar complex types themselves, with results of varying quality, that may miss important edge cases or perform much worse than an optimal implementation would. I understand that there may have to be similar limitations on a built-in type, for recursive or excessively-deep inputs, but these could at least be thoughtfully designed and documented, with useful error messages.
π» Use Cases
- https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object
- https://www.reddit.com/r/typescript/comments/iywewf/ts41_is_there_a_way_to_define_this_property_path/
- TypeScript 4.1+: Generic binding too broad in recursive conditional typesΒ #41380
- Reactive forms are not strongly typedΒ angular/angular#13721
- Support dotted path notationΒ millsp/ts-toolbelt#154