Skip to content
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

Merged
merged 3 commits into from
Dec 13, 2024
Merged

feat: Add AtLeastOne type #171

merged 3 commits into from
Dec 13, 2024

Conversation

Drodevbar
Copy link
Contributor

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

  • Apply one of following labels; major, minor, patch or skip-release
  • I've updated the documentation, or no changes were necessary
  • I've updated the tests, or no changes were necessary

@Drodevbar Drodevbar force-pushed the feat/add-at-least-one-type branch from 485154b to 2749d2e Compare December 12, 2024 13:57
@Drodevbar Drodevbar force-pushed the feat/add-at-least-one-type branch from 8a2585e to aab5016 Compare December 13, 2024 09:57
@@ -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]
Copy link
Collaborator

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?

Copy link
Contributor Author

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)
Copy link
Collaborator

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?

Copy link
Contributor Author

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

@Drodevbar Drodevbar force-pushed the feat/add-at-least-one-type branch from c22d0c6 to 4a9c017 Compare December 13, 2024 11:27
@Drodevbar Drodevbar requested a review from kibertoad December 13, 2024 11:33
Copy link
Collaborator

@kibertoad kibertoad left a 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!

@Drodevbar Drodevbar merged commit 37c3eac into main Dec 13, 2024
5 checks passed
@Drodevbar Drodevbar deleted the feat/add-at-least-one-type branch December 13, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants