-
-
Notifications
You must be signed in to change notification settings - Fork 230
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
feat: add Optional type with granular selection by key #82
Conversation
That's nice, but why not just use more generic type Person = Optional<Example>; // all props are optional
type Person = Optional<Example, 'age' | 'height'>; // only selected keys are optional |
@piotrwitek oh, you are right!I did it, please review once ? |
src/mapped-types.ts
Outdated
* // Expect: { name: string; age?: string; height?: number; } | ||
* type Props = Optional<Props, 'age' | 'height'>; | ||
*/ | ||
export type Optional<T extends {}, K = keyof any> = K extends (keyof T) |
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.
Please change {}
to object
as this is a convention used in this project
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.
done
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.
It looks great! But we still need to add the missing parts:
[ ] I have added a short example in API Docs to demonstrate new usage --> you can copy what you already added in the JSDoc comments
[ ] I have added type unit tests with dts-jest --> in .spec.ts
file just copy tests from other function and try to adapt it for new Optional type, the rest will happen automatically on push
@piotrwitek maybe all is ready? |
@ShanaMaid awesome job, thanks! (Pick<Props, "name" | "visible"> & { age?: number | undefined; }) | (Pick<Props, "name" | "age"> & { visible?: boolean | undefined; }) I'll take a look in 1 hour |
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.
Hey @ShanaMaid, I'm merging it now, thanks for the awesome job 🎉
@ShanaMaid I have simplified the Optional type, now it has much clearer resulting type: export type Optional<T extends object, K extends keyof T = keyof T> =
Omit<T,K> & Partial<Pick<T, K>>; I'll be releasing in v3.7 |
ok, that's cool! |
Can we get the inverse to, i.e. |
@felixfbecker definitely yes, as this type turned out to be quite useful. |
Description
Related issues:
Checklist
For bugfixes:
For new features:
dts-jest
I add
PartOptional