Last active
September 25, 2024 02:37
-
-
Save reececomo/715de774233942f21b4b18fbd3e19d29 to your computer and use it in GitHub Desktop.
AllowExcess<T>, InexactOptionals<T>, Untrusted<T>, etc.
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
| /* eslint-disable @typescript-eslint/ban-types */ | |
| /** | |
| * Allow object literals to include additional unchecked properties. | |
| * @see https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks | |
| */ | |
| // prettier-ignore | |
| export type AllowExcess<T> = T extends object | |
| ? T extends Array<infer U> ? AllowExcess<U>[] | |
| : {[K in keyof T]: AllowExcess<T[K]>} & {[key: string]: unknown} | |
| : T; | |
| /** | |
| * Make all undefined-typed properties in objects optional, recursively. | |
| * @see https://www.typescriptlang.org/tsconfig/exactOptionalPropertyTypes.html | |
| */ | |
| // prettier-ignore | |
| export type InexactOptionals<T> = T extends object | |
| ? T extends Array<infer V> ? InexactOptionals<V>[] | |
| : {[K in keyof T as undefined extends T[K] ? never : K]: InexactOptionals<T[K]>} & | |
| {[K in keyof T as undefined extends T[K] ? K : never]?: InexactOptionals<T[K]>} | |
| : T; | |
| /** | |
| * Make all null-type properties in objects optional, recursively. | |
| */ | |
| // prettier-ignore | |
| export type OptionalNullables<T> = T extends object | |
| ? T extends Array<infer V> ? OptionalNullables<V>[] | |
| : {[K in keyof T as null extends T[K] ? never : K]: OptionalNullables<T[K]>} & | |
| {[K in keyof T as null extends T[K] ? K : never]?: OptionalNullables<T[K]>} | |
| : T; | |
| /** | |
| * Make all undefined-type properties in objects nullable, recursively. | |
| */ | |
| // prettier-ignore | |
| export type NullableOptionals<T> = T extends object | |
| ? T extends Array<infer V> ? NullableOptionals<V>[] | |
| : {[K in keyof T as undefined extends T[K] ? never : K]: NullableOptionals<T[K]> | null} & | |
| {[K in keyof T as undefined extends T[K] ? K : never]?: NullableOptionals<T[K]> | null} | |
| : T; | |
| /** | |
| * Make intellisense on complex objects simpler, recursively. | |
| * @see https://www.totaltypescript.com/concepts/the-prettify-helper | |
| */ | |
| // prettier-ignore | |
| export type Pretty<T> = T extends object ? {[K in keyof T]: Pretty<T[K]>} & {} : T; | |
| /** | |
| * Make all properties in objects optional, recursively. | |
| */ | |
| // prettier-ignore | |
| export type Untrusted<T> = T extends object | |
| ? T extends Array<infer ValueType> ? Untrusted<ValueType>[] | |
| : {[K in keyof T]?: Untrusted<T[K]>} | |
| : T; | |
| /** | |
| * Make all properties in objects optional and nullable, recursively. | |
| */ | |
| // prettier-ignore | |
| export type UntrustedNullable<T> = T extends object | |
| ? T extends Array<infer ValueType> ? UntrustedNullable<ValueType>[] | |
| : {[K in keyof T]?: UntrustedNullable<T[K]> | null} | |
| : T; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cc @Not-Jayden