Created
May 16, 2019 23:37
-
-
Save prmichaelsen/39f4381dbe7ce617870028485aa74c3f to your computer and use it in GitHub Desktop.
recursively mark all properties of a typescript type as defined
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
/** private type, not exported */ | |
declare type NonObject = undefined | null | boolean | string | number | Function; | |
/** | |
* This type allows you to mark an object with | |
* optional properties as required. | |
*/ | |
export declare type Complete<T> = { | |
[K in keyof T]-?: T[K]; | |
} | |
/** | |
* This type allows you to mark an object with | |
* optional properties as required recursively. | |
*/ | |
export declare type DeepComplete<T> = | |
T extends NonObject ? | |
T extends undefined ? never : T | |
: | |
T extends Array<infer U> ? | |
DeepCompleteArray<U> | |
: | |
T extends Map<infer K, infer V> ? | |
DeepCompleteMap<K, V> | |
: | |
DeepCompleteObject<T>; | |
interface DeepCompleteArray<T> extends ReadonlyArray<DeepComplete<T>> { } | |
interface DeepCompleteMap<K, V> extends ReadonlyMap<DeepComplete<K>, DeepComplete<V>> { } | |
declare type DeepCompleteObject<T> = { | |
[K in keyof T]-?: DeepComplete<T[K]>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment