Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f0e772
commit 1a69e63
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
import type {Except} from './except'; | ||
import type {Simplify} from './simplify'; | ||
|
||
/** | ||
Create a type that makes the given keys non-nullable. The remaining keys are kept as is. | ||
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are non-nullable. | ||
@example | ||
``` | ||
import type {SetNonNullable} from 'type-fest'; | ||
type Foo = { | ||
a: number; | ||
b: string | undefined; | ||
c?: boolean | null; | ||
} | ||
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>; | ||
// type SomeNonNullable = { | ||
// a: number; | ||
// b: string; // Can no longer be undefined. | ||
// c?: boolean; // Can no longer be null, but is still optional. | ||
// } | ||
``` | ||
@category Object | ||
*/ | ||
export type SetNonNullable<BaseType, Keys extends keyof BaseType> = | ||
Simplify< | ||
// Pick just the keys that are readonly from the base type. | ||
Except<BaseType, Keys> & | ||
// Pick the keys that should be non-nullable from the base type and make them non-nullable. | ||
{[Key in Keys]: NonNullable<BaseType[Key]>} | ||
>; |
This file contains 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,18 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import type {SetNonNullable} from '../index'; | ||
|
||
// Update one possibly undefined key and one possibly null key to non-nullable. | ||
declare const variation1: SetNonNullable<{a: number; b: string | undefined; c: boolean | null}, 'b' | 'c'>; | ||
expectType<{a: number; b: string; c: boolean}>(variation1); | ||
|
||
// Update a key that is possibly null or undefined. | ||
declare const variation2: SetNonNullable<{a: number; b: string | null | undefined}, 'b'>; | ||
expectType<{a: number; b: string}>(variation2); | ||
|
||
// Update an optional key. | ||
declare const variation3: SetNonNullable<{a: number; b?: string | undefined}, 'b'>; | ||
expectType<{a: number; b?: string}>(variation3); | ||
|
||
// Fail if type changes even if non-nullable is right. | ||
declare const variation4: SetNonNullable<{a: number; b: string | undefined}, 'b'>; | ||
expectError<{a: string; b: string}>(variation4); |