-
-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: deep version of StringKeyOf #432
Comments
How is a union of all key paths useful for this? |
We already have a |
Hey,
A simple example : I have a generic type with a prop ( interface ColumnDefinition<T> {
id: string;
label: string;
key: StringKeyOf<T> // or keyof T
} I have an interface representing my object, with nested properties interface MyObject {
_id: string;
name: string;
customData: {
type: string;
enabled: boolean;
};
} Then, when using my generic type type MyObjectColumnDefinition = ColumnDefinition<MyObject>;
// here, key will be : '_id' | 'name' | 'customData'
// with a deep version it could have been : '_id' | 'name' | 'customData' | 'customData.type' | 'customData.type.enabled' adding deepProperties to union I don't think the |
Additionally, it would be nice to have features like the Path type in React Hook Form. 🙏 It also supports arrays. (It may deviate from StringKeyOfDeep, but...) type Post = {
id: string;
title: string;
comments: {
id: string;
body: string;
}[]
}
type fieldType = Path<Post>;
// type fieldType = "id" | "title" | "comments" | `comments.${number}` | `comments.${number}.id` | `comments.${number}.body` Thanks for maintaining a great library! |
A leaves-only version of this would also be really useful: type Post = {
id: string;
title: string;
comments: {
id: string;
body: string;
}[]
}
type fieldType = Path<Post, { onlyLeaves: true }>;
// type fieldType = "id" | "title" | `comments.${number}.id` | `comments.${number}.body` |
I was hoping However, another library I am using, react-hook-form, has a similar concept util type call /**
* Helper type for recursively constructing paths through a type.
* See {@link Path}
*/
declare type PathImpl<K extends string | number, V> = V extends Primitive | BrowserNativeObject ? `${K}` : `${K}` | `${K}.${Path<V>}`;
/**
* Type which eagerly collects all paths through a type
* @typeParam T - type which should be introspected
* @example
* ```
* Path<{foo: {bar: string}}> = 'foo' | 'foo.bar'
* ```
*/
export declare type Path<T> = T extends ReadonlyArray<infer V> ? IsTuple<T> extends true ? {
[K in TupleKeys<T>]-?: PathImpl<K & string, T[K]>;
}[TupleKeys<T>] : PathImpl<ArrayKey, V> : {
[K in keyof T]-?: PathImpl<K & string, T[K]>;
}[keyof T]; |
Duplicate of #213 |
This is accepted. The type should be named |
I agree it should include a |
See the initial attempt at adding this here: #300 |
Don't forget to add |
Relevant Stack Overflow answer: https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object |
Also some inspiration in #213 |
I think this was completed in #741 |
@voxpelli i think one thing still open is a "leaves only" option, see #432 (comment) |
@stefanprobst Lets keep track of that in #860, easier to pinpoint then. Does that sound good? |
perfect, thanks! |
Hey,
It could be great to have a deep version of the utility type StringKeyOf that would extracts nested keys with dot-notation.
Pretty useful when working with libraries using dot-notation to access values or to manipulate types.
Example
Upvote & Fund
The text was updated successfully, but these errors were encountered: