-
-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge
: Fix optional keys and type override (#456)
- Loading branch information
Showing
4 changed files
with
122 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type {Simplify} from './simplify'; | ||
|
||
// Returns `never` if the key is optional otherwise return the key type. | ||
type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key] | ||
? Type[Key] extends undefined | ||
? Key | ||
: never | ||
: Key; | ||
|
||
// Returns `never` if the key is required otherwise return the key type. | ||
type OptionalFilter<Type, Key extends keyof Type> = undefined extends Type[Key] | ||
? Type[Key] extends undefined | ||
? never | ||
: Key | ||
: never; | ||
|
||
/** | ||
Enforce optional keys (by adding the `?` operator) for keys that have a union with `undefined`. | ||
@example | ||
``` | ||
import type {Merge} from 'type-fest'; | ||
type Foo = { | ||
a: string; | ||
b?: string; | ||
c: undefined; | ||
d: number | undefined; | ||
}; | ||
type FooBar = EnforceOptional<Foo>; | ||
// => { | ||
// a: string; | ||
// b?: string; | ||
// c: undefined; | ||
// d?: number; | ||
// } | ||
``` | ||
@internal | ||
@category Object | ||
*/ | ||
export type EnforceOptional<ObjectType> = Simplify<{ | ||
[Key in keyof ObjectType as RequiredFilter<ObjectType, Key>]: ObjectType[Key] | ||
} & { | ||
[Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<ObjectType[Key], undefined> | ||
}>; |
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,20 @@ | ||
import {expectType} from 'tsd'; | ||
import type {EnforceOptional} from '../source/enforce-optional'; | ||
|
||
type Foo = { | ||
a: string; | ||
b?: string; | ||
c: undefined; | ||
d: number | undefined; | ||
}; | ||
|
||
type EnforcedOptionalFoo = EnforceOptional<Foo>; | ||
|
||
declare const enforcedOptionalFoo: EnforcedOptionalFoo; | ||
|
||
expectType<{ | ||
a: string; | ||
b?: string; | ||
c: undefined; | ||
d?: number; | ||
}>(enforcedOptionalFoo); |
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