-
Notifications
You must be signed in to change notification settings - Fork 1
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 AtLeastOne type #171
Conversation
485154b
to
2749d2e
Compare
8a2585e
to
aab5016
Compare
@@ -0,0 +1,2 @@ | |||
// can be used when you want to make at least one key of an object required | |||
export type AtLeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U] |
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.
can you add documentation for it to readme.md?
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've added them to docs/custom-types.md
as I see we have some documents already there. I've included the existing types that haven't been documented so far.
const testCase2: AtLeastOne<TestType> = { b: 1 } | ||
|
||
assertType<{ a: string; b?: number }>(testCase1) | ||
assertType<{ a?: string; b: number }>(testCase2) |
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.
can you also add negative case?
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.
Added negative test ✅
I've also enabled typecheck
as by default vitest has it disabled - please see vitest.config.mts
c22d0c6
to
4a9c017
Compare
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.
thank you, this is great!
Changes
I find this util type useful when I have a function that requires me to pass at least one param. Thanks to it, I don't have to manually verify whether one of them was passed, but TS can do the work.
Checklist
major
,minor
,patch
orskip-release