Last active
May 3, 2023 16:10
-
-
Save Wintus/741db3cda1e699dc2686baadab56adec to your computer and use it in GitHub Desktop.
TypeScript: type-level programming and some helpers
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 { DeepNonNullable, Falsy, isFalsy } from "utility-types"; | |
export const isNullable = <T>(value: Nullable<T>): value is null | undefined => value == null; | |
export const assertIsDefined = <T>(val: T): asserts val is NonNullable<T> => { | |
if (isNullable(val)) { | |
throw new Error(`Expected 'val' to be defined, but received ${val}`); | |
} | |
}; | |
type Truthy<T> = Exclude<T, Falsy>; | |
export const assertTruthy = <T>(value: T, message?: string): asserts value is Truthy<T> => { | |
if (isFalsy(value)) { | |
throw new Error(message); | |
} | |
}; |
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
type Async<F extends (...args: any[]) => any> = (...args: Parameters<F>) => Promise<ReturnType<F>>; |
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
type Defined<T> = Exclude<T, undefined>; |
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
// https://github.com/Microsoft/TypeScript/issues/27024 | |
export type Equals<X, Y> = | |
(<T>() => T extends X ? 1 : 0) extends | |
(<T>() => T extends Y ? 1 : 0) ? true : false; |
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
export type IndexSignature<O extends object> = { | |
[P in keyof O]: O[P]; | |
}; |
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
type InferIntersection<I, T> = I extends (T & infer U) ? U : never; |
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
type Nullable<T> = T | null; | |
const nonNull = <T>(x?: Nullable<T>): boolean = x != null; |
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
type ShouldNotBeNull<T> = 1 extends (T extends null ? 1 : never) ? never : T; | |
function f<K>(x: ShouldNotBeNull<K>): void { x; } | |
f<number>(1) | |
// @ts-expect-error: TS2345 | |
f<number|null>(1) |
type AndExtends<T, A, B> = any extends T ? A : B
type AndExtends<T, A, B> = A | B; // T != any
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref