-
-
Notifications
You must be signed in to change notification settings - Fork 714
Add SetNullable type
#1356
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
Open
derodero24
wants to merge
5
commits into
sindresorhus:main
Choose a base branch
from
derodero24:add/set-nullable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
Add SetNullable type
#1356
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
034c712
Add `SetNullable` type
derodero24 244b33c
fix: JSDoc twoslash comment
som-sm f59d605
`SetNullable`: Add tests for unions, index signatures, and `any`/`nev…
derodero24 66aa3fc
test: add case
som-sm 605dda1
fix: remove extra new line at end
som-sm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| Create a type that makes the given keys nullable, where the remaining keys are kept as is. | ||
|
|
||
| If no keys are given, all keys will be made nullable. | ||
|
|
||
| Use-case: You want to define a single model where the only thing that changes is whether or not some or all of the keys are nullable. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {SetNullable} from 'type-fest'; | ||
|
|
||
| type Foo = { | ||
| a: number; | ||
| b: string; | ||
| c?: boolean; | ||
| }; | ||
|
|
||
| type SomeNullable = SetNullable<Foo, 'a' | 'c'>; // Optionality is preserved for `c` | ||
| //=> {a: number | null; b: string; c?: boolean | null} | ||
|
|
||
| type AllNullable = SetNullable<Foo>; | ||
| //=> {a: number | null; b: string | null; c?: boolean | null} | ||
| ``` | ||
|
|
||
| @see {@link SetNonNullable} | ||
|
|
||
| @category Object | ||
| */ | ||
| export type SetNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = { | ||
| [Key in keyof BaseType]: Key extends Keys | ||
| ? BaseType[Key] | null | ||
| : BaseType[Key]; | ||
| }; | ||
|
|
||
| export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import {expectNotAssignable, expectType} from 'tsd'; | ||
| import type {SetNullable} from '../index.d.ts'; | ||
|
|
||
| // Make one key nullable. | ||
| declare const variation1: SetNullable<{a: number; b: string}, 'a'>; | ||
| expectType<{a: number | null; b: string}>(variation1); | ||
|
|
||
| // Make multiple keys nullable. | ||
| declare const variation2: SetNullable<{a: number; b: string; c: boolean}, 'a' | 'c'>; | ||
| expectType<{a: number | null; b: string; c: boolean | null}>(variation2); | ||
|
|
||
| // Preserve optional modifier. | ||
| declare const variation3: SetNullable<{a: number; b?: string}, 'b'>; | ||
| expectType<{a: number; b?: string | null}>(variation3); | ||
|
|
||
| // Key already nullable remains unchanged. | ||
| declare const variation4: SetNullable<{a: number | null; b: string}, 'a'>; | ||
| expectType<{a: number | null; b: string}>(variation4); | ||
|
|
||
| // Fail if type changes even if nullable is right. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYM by this? What's failing here? |
||
| declare const variation5: SetNullable<{a: number; b: string}, 'b'>; | ||
| expectNotAssignable<{a: string; b: string | null}>(variation5); | ||
|
|
||
| // Make all keys nullable when `Keys` generic is not passed. | ||
| declare const variation6: SetNullable<{a: number; b: string; c: boolean}>; | ||
| expectType<{a: number | null; b: string | null; c: boolean | null}>(variation6); | ||
|
|
||
| // Preserve readonly modifier. | ||
| declare const variation7: SetNullable<{readonly a: number; b: string}, 'a'>; | ||
| expectType<{readonly a: number | null; b: string}>(variation7); | ||
|
|
||
| // Works with unions. | ||
| declare const variation8: SetNullable<{a: '1'; b: string; c: boolean} | {a: '2'; b: string; c: boolean}, 'a' | 'b'>; | ||
| expectType<{a: '1' | null; b: string | null; c: boolean} | {a: '2' | null; b: string | null; c: boolean}>(variation8); | ||
|
|
||
| // Works with index signatures. | ||
| declare const variation9: SetNullable<{[k: string]: unknown; a: number; b: string}, 'a'>; | ||
| expectType<{[k: string]: unknown; a: number | null; b: string}>(variation9); | ||
|
|
||
| declare const variation9a: SetNullable<{[k: Uppercase<string>]: number; A: number; b: string}, Uppercase<string>>; | ||
| expectType<{[k: Uppercase<string>]: number | null; A: number | null; b: string}>(variation9a); | ||
|
|
||
| // Makes all keys nullable when `Keys` is `any`. | ||
| declare const variation10: SetNullable<{readonly a: number; b?: string; c: boolean}, any>; | ||
| expectType<{readonly a: number | null; b?: string | null; c: boolean | null}>(variation10); | ||
|
|
||
| // Does nothing when `Keys` is `never`. | ||
| declare const variation11: SetNullable<{a: number; readonly b?: string; readonly c: boolean}, never>; | ||
| expectType<{a: number; readonly b?: string; readonly c: boolean}>(variation11); | ||
|
|
||
|
som-sm marked this conversation as resolved.
|
||
| // Preserves existing `undefined` when adding `null`. | ||
| declare const variation12: SetNullable<{a: string | undefined; b: number}, 'a'>; | ||
| expectType<{a: string | undefined | null; b: number}>(variation12); | ||
|
|
||
| // Works with unions where only some keys are shared across branches. | ||
| declare const variation13: SetNullable<{a: number; c: boolean} | {a: string; d: boolean}, 'a'>; | ||
| expectType<{a: number | null; c: boolean} | {a: string | null; d: boolean}>(variation13); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.