Last active
July 31, 2023 06:55
-
-
Save kaareloun/bd8c829d1e497d114827cad20f8b59db to your computer and use it in GitHub Desktop.
Typescript utilities
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
import "@total-typescript/ts-reset"; | |
export {} | |
// Makes all properties optional | |
export type DeepPartial<T> = T extends object | |
? { | |
[P in keyof T]?: DeepPartial<T[P]> | |
} | |
: T | |
// Type with only selected properties | |
export type Only<T, U> = { | |
[P in keyof T]: T[P] | |
} & { | |
[P in keyof U]?: never | |
} | |
// One or the other | |
export type Either<T, U> = Only<T, U> | Only<U, T> | |
// Allow array.filter(Boolean), already handled in | |
// https://github.com/total-typescript/ts-reset | |
type Falsy = false | 0 | '' | null | undefined | |
interface Array<T> { | |
filter<S extends T>( | |
predicate: BooleanConstructor, | |
thisArg?: any | |
): Exclude<S, Falsy>[] | |
} | |
// Make chosen fields required. Useful when fetching data that | |
// has different relations loaded in different endpoints | |
export type WithRequired<TType, TRequiredKeys extends keyof TType> = TType & { | |
[P in TRequiredKeys]-?: TType[P] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment