-
-
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
Add SetRequiredDeep
type
#939
base: main
Are you sure you want to change the base?
Conversation
Can this feature be added on @sindresorhus how about you think? |
The problem with doing both shallow and deep in a single method is that the deep logic affects the performance even if you don't need deep. This was the case for |
}[] | ||
} | ||
|
||
type SomeRequiredDeep = SetRequiredDeep<Foo, 'a' | 'array.c'>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type SomeRequiredDeep = SetRequiredDeep<Foo, 'a' | 'array.c'>; | |
type SomeRequiredDeep = SetRequiredDeep<Foo, 'a' | 'array.${number}.c'>; |
I kinda forgot how it's should be. You can add the example in test to ensure it is worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda forgot how it's should be. You can add the example in test to ensure it is worked.
This doesn't seem to be resolved @hugomartinet
ffc216c
to
1c4f238
Compare
1c4f238
to
e3c2874
Compare
Need to resolve all conversation |
Done |
|
||
// Update a root key to required. | ||
declare const variation2: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'a'>; | ||
expectType<{a: number; b?: {c?: string}}>(variation2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth adding a test case with multiple paths (i.e. BaseType
is not a union)?
declare const variation2: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'a' | 'b'>;
expectType<{a: number; b: {c?: string}}>(variation2);
|
||
@category Object | ||
*/ | ||
export type SetRequiredDeep<BaseType, KeyPaths extends Paths<BaseType>> = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm playing with this solution using the following example:
type Teacher = { address?: { postcode?: string; city?: string } };
type P1 = SetRequiredDeep<Teacher, 'address' | 'address.city'>;
// ^?
This gives me:
type P1 = {
address: {
postcode: string;
city: string;
};
} | {
address: {
postcode: string;
city: string;
};
}
Is the result expected?
To elaborate:
- Why is the postcode required?
- Why is the result object a union? Are there any plans to merge a union into a single object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, similar to PickDeep
and OmitDeep
. result should be
type P1 = {
address: {
postcode?: string;
city: string;
};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think my current implementation could not work for this, since I use RequiredDeep
and RequiredDeep
makes all child keys of an object required as well.
Closes #796
This pull request adds a new
SetRequiredDeep
which, likeSetRequired
, allows to make one or several keys in a model required. It adds the possibility to select nested keys by specifying their path.Example