Skip to content

Instantly share code, notes, and snippets.

@garmjs
Created April 27, 2023 21:50
Show Gist options
  • Save garmjs/9135910c6b70d6ff479ef650b54d2295 to your computer and use it in GitHub Desktop.
Save garmjs/9135910c6b70d6ff479ef650b54d2295 to your computer and use it in GitHub Desktop.
Type Utils for TypeScript from totaltypescript.com
export type Expect<T extends true> = T;
export type ExpectTrue<T extends true> = T;
export type ExpectFalse<T extends false> = T;
export type IsTrue<T extends true> = T;
export type IsFalse<T extends false> = T;
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
? true
: false;
export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IsAny<T> = 0 extends 1 & T ? true : false;
export type NotAny<T> = true extends IsAny<T> ? false : true;
export type Debug<T> = { [K in keyof T]: T[K] };
export type MergeInsertions<T> = T extends object
? { [K in keyof T]: MergeInsertions<T[K]> }
: T;
export type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
export type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE
? true
: false;
export type ExpectValidArgs<
FUNC extends (...args: any[]) => any,
ARGS extends any[],
> = ARGS extends Parameters<FUNC> ? true : false;
export type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment