Created
August 2, 2019 08:29
-
-
Save RomkeVdMeulen/cf154312f9cc10d09fbad62334f685ab to your computer and use it in GitHub Desktop.
Some advanced partial object type definitions for TypeScript
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
// tslint:disable:align ban-types | |
export type DeepPartial<T> = | |
T extends Array<infer U> ? DeepPartialArray<U> : | |
T extends ReadonlyArray<infer V> ? DeepPartialReadonlyArray<V> : | |
T extends object ? DeepPartialObject<T> : | |
T; | |
export type DeepPartialNoMethods<T> = | |
T extends Array<infer U> ? DeepPartialArrayNoMethods<U> : | |
T extends ReadonlyArray<infer V> ? DeepPartialReadonlyArrayNoMethods<V> : | |
T extends object ? DeepPartialObjectNoMethods<T> : | |
T; | |
export interface DeepPartialArrayNoMethods<T> extends Array<DeepPartialNoMethods<T>> {} | |
export interface DeepPartialArray<T> extends Array<DeepPartial<T>> {} | |
export interface DeepPartialReadonlyArrayNoMethods<T> extends ReadonlyArray<DeepPartialNoMethods<T>> {} | |
export interface DeepPartialReadonlyArray<T> extends ReadonlyArray<DeepPartial<T>> {} | |
export type DeepPartialObject<T> = { | |
[P in keyof T]?: DeepPartial<T[P]>; | |
}; | |
export type NonFunctionPropertyNames<T> = NonNullable<{ | |
[P in keyof T]: T[P] extends Function ? never : P; | |
}[keyof T]>; | |
export type DeepPartialObjectNoMethods<T> = { | |
[P in NonFunctionPropertyNames<T>]?: DeepPartialNoMethods<T[P]>; | |
}; | |
export type DeepReadonly<T> = | |
T extends Array<infer U> ? DeepReadonlyArray<U> : | |
T extends Function ? T : | |
T extends object ? DeepReadonlyObject<T> : | |
T; | |
export interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {} | |
export type DeepReadonlyObject<T> = { | |
readonly [P in keyof T]: DeepReadonly<T[P]>; | |
}; | |
export type ObjectInitializer<T> = DeepPartialNoMethods<DeepReadonly<T>>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment