Skip to content

Instantly share code, notes, and snippets.

@kaareloun
Last active July 31, 2023 06:55
Show Gist options
  • Save kaareloun/bd8c829d1e497d114827cad20f8b59db to your computer and use it in GitHub Desktop.
Save kaareloun/bd8c829d1e497d114827cad20f8b59db to your computer and use it in GitHub Desktop.
Typescript utilities
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