-
Notifications
You must be signed in to change notification settings - Fork 118
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
Deep analogue of MarkRequired with the skeleton of DeepModify #315
Comments
Hey @xenoterracide! Thank you for the suggestion! Yeah, it's a good feature that can be implemented |
I started working on it here – #329 It will take time, so it will be possibly available in the release after the next one |
I think I will create a separate |
I'd love this too -- I have this need right now :) |
@jrnail23 hey! currently, it's a part of the next v10.1 release so it's unlikely to get merged today and be available. But I can have a look at the implementation now. I will come back to you when I have it. |
After reflecting on the proposal, I've realised that the A simple example would be: // There is a teacher with all required properties
type Teacher = {
name: string;
gender: "M" | "F"; // etc
address: {
postcode: string;
city: string;
street: string;
apt: string;
};
};
// Developers have to validate the object from the backend, therefore all properties are marked as optional
type MaybeTeacher = DeepPartial<Teacher>;
// But some of the properties have to be required for business logic, e.g. a city where a teacher lives
type TeacherWithAddress = DeepMarkRequired<
MaybeTeacher,
{
address: {
city: never;
};
}
>; What is expected to get in // 1. Only city is a required property while address will be optional
// {
// name?: string;
// gender?: "M" | "F"; // etc
// address?: { // address is NOT required 🔴
// postcode?: string;
// city: string; // city is required 🟢
// street?: string;
// apt?: string;
// };
// }
// 2. Both address and city will be required
// {
// name?: string;
// gender?: "M" | "F"; // etc
// address: { // address is required 🟢
// postcode?: string;
// city: string; // city is required 🟢
// street?: string;
// apt?: string;
// };
// } Because the syntax is ambiguous, it will definitely confuse engineers during development. One possible workaround is to use type TeacherWithAddress = DeepMarkRequired<
MaybeTeacher,
{
address: true | {
city: never;
};
}
>; However, I agree that it's not the best syntax I suggest an alternative syntax with union elements: type TeacherSimple = DeepMarkRequired<
MaybeTeacher,
'address' | 'address.city'
>;
@jrnail23 @xenoterracide @DustinJSilk What do you think? |
Meanwhile, I've improved This will provide better DX if |
@jrnail23 @xenoterracide @DustinJSilk my proposal is implemented in #408 If you'd like to test the solution, please have a look at the TypeScript Playground - https://tsplay.dev/w1qRGw. It's not production-ready yet but I'm happy to experiment with it. Any feedback is welcome! |
I would like to be able to provide a structure to
DeepRequired
like DeepOmit, and I suspect this would be useful to all the Deep nested. The same structure asDeepOmit
would work.The text was updated successfully, but these errors were encountered: