tsdef
TypeScript common patterns shortcuts definitions snippets utility gist library
Get Started
npm install -D tsdef
or
yarn add -D tsdef
How to Use
import { Nullable, NonNull } from 'tsdef';
const nullableString: Nullable<string> = null;
const nonNullString: NonNull<string | null> = null;
export type nil = null | undefined;
export type Nilable<T> = T | nil;
export type Nullable<T> = T | null;
export type Undefinable<T> = T | undefined;
export type MaybeNil<T> = T | nil;
export type MaybeNull<T> = T | null;
export type MaybeUndefined<T> = T | undefined;
export type MaybePromise<T> = T | Promise<T>;
export type MaybeArray<T> = T | T[];
export type MaybeAsReturnType<T> = T | ((...args: any) => T);
export type NonNil<T> = T extends nil ? never : T;
export type NonNull<T> = T extends null ? never : T;
export type NonUndefined<T> = T extends undefined ? never : T;
export type NilableProps<T> = { [P in keyof T]?: T[P] | nil };
export type NullableProps<T> = { [P in keyof T]: T[P] | null };
export type UndefinableProps<T> = { [P in keyof T]?: T[P] | undefined };
export type NonNilProps<T> = { [P in keyof T]-?: NonNil<T[P]> };
export type NonNullProps<T> = { [P in keyof T]: NonNull<T[P]> };
export type NonUndefinedProps<T> = { [P in keyof T]-?: NonUndefined<T[P]> };
export type RequiredProps<T> = { [P in keyof T]-?: T[P] };
export type AnyKey = keyof any;
export type AnyFunction = (...args: any) => any;
export type AnyConstructor = new (...args: any) => any;
export interface AnyClass {
prototype: any;
new (...args: any): any;
}
export interface AnyPrototype {
constructor: any;
}
export interface AnyObject {
[key: string]: any;
[key: number]: any;
}
export interface AnyObjectWithStringKeys {
[key: string]: any;
}
export interface AnyObjectWithNumberKeys {
[key: number]: any;
}
export type ExcludeKeys<T, K extends AnyKey> = Omit<T, K>;
export type ValueOf<T> = T[keyof T];
export type Property<T, K> = K extends keyof T ? T[K] : never;
export type KeyOfType<T, U> = {
[P in keyof T]-?: T[P] extends U ? P : never
}[keyof T];
export type KeyOfSubType<T, U> = {
[P in keyof T]-?: U extends T[P] ? P : never
}[keyof T];
export type WithOptionalKeys<T, K extends keyof T> = Omit<T, K> &
Partial<Pick<T, K>>;
export type WithNilableKeys<T, K extends keyof T> = Omit<T, K> &
NilableProps<Pick<T, K>>;
export type WithNullableKeys<T, K extends keyof T> = Omit<T, K> &
NullableProps<Pick<T, K>>;
export type WithUndefinableKeys<T, K extends keyof T> = Omit<T, K> &
UndefinableProps<Pick<T, K>>;
export type WithNonNilKeys<T, K extends keyof T> = Omit<T, K> &
NonNilProps<Pick<T, K>>;
export type WithNonNullKeys<T, K extends keyof T> = Omit<T, K> &
NonNullProps<Pick<T, K>>;
export type WithNonUndefinedKeys<T, K extends keyof T> = Omit<T, K> &
NonUndefinedProps<Pick<T, K>>;
export type DiffObjects<T, U> = Omit<T, keyof U>;
export type UnionObjects<
T extends AnyObject,
U extends AnyObject
> = DiffObjects<T, U> &
{ [P in keyof T & keyof U]: T[P] | U[P] } &
DiffObjects<U, T>;
export type OverwriteProps<T, U> = U & DiffObjects<T, U>;
export type Arguments<T extends AnyFunction> = Parameters<T>;
export type FirstArgument<T extends AnyFunction> = T extends (
arg: infer A,
...args: any
) => any
? A
: never;
export type Return<T extends AnyFunction> = ReturnType<T>;
export type MaybeReturnType<T> = T extends (...args: any[]) => infer R ? R : T;
export type InstanceOf<T extends AnyConstructor> = InstanceType<T>;
export type PromisedType<T extends Promise<any>> = T extends Promise<infer R>
? R
: never;
export type MaybePromisedType<T> = T extends Promise<infer R> ? R : T;
export type MaybeAsyncReturnType<T extends AnyFunction> = MaybePromisedType<
ReturnType<T>
>;
export type ItemType<T extends any[]> = T extends Array<infer I> ? I : never;
export type Thunk<T> = () => T;
export type MaybeThunk<T> = T | Thunk<T>;
export type Unthunk<T extends Thunk<any>> = T extends Thunk<infer R>
? R
: never;
export type MaybeUnthunk<T> = T extends Thunk<infer R> ? R : T;
export type Unpack<T> = T extends Array<infer I>
? I
: T extends (...args: any) => infer R
? R
: T extends Promise<infer P>
? P
: T;
export type InheritClass<C1 extends AnyClass, C2 extends AnyClass> = {
prototype: OverwriteProps<C2['prototype'], C1['prototype']>;
new (...args: ConstructorParameters<AnyClass>): OverwriteProps<
C2['prototype'],
C1['prototype']
>;
} & OverwriteProps<C2, C1>;
export type IsNonNil<T, True, False = never> = null extends T
? False
: undefined extends T
? False
: True;
export type IsNonNull<T, True, False = never> = null extends T ? False : True;
export type IsNonUndefined<T, True, False = never> = undefined extends T
? False
: True;
export type IsNever<T, True, False = never> = [T] extends [never]
? True
: False;
export type IsAny<T, True, False = never> = (
| True
| False) extends (T extends never ? True : False)
? True
: False;
export type IsUnknown<T, True, False = never> = unknown extends T
? IsAny<T, False, True>
: False;
export type StrictlyIncludes<T, U, True, False = never> = Exclude<
U,
T
> extends never
? (IsAny<T, 1, 0> extends 1
? True
: (IsAny<U, 1, 0> extends 1
? False
: (IsUnknown<T, 1, 0> extends 1 ? IsUnknown<U, True, False> : True)))
: False;
export type AreStrictlyEqual<T, U, True, False = never> = StrictlyIncludes<
T,
U,
1,
0
> extends 1
? StrictlyIncludes<U, T, True, False>
: False;
export type HaveSameKeys<T, U, True, False = never> = (
| Exclude<keyof T, keyof U>
| Exclude<keyof U, keyof T>) extends never
? True
: False;
export type Exact<T, X extends T> = T &
{ [K in keyof X]: K extends keyof T ? X[K] : never };