TypeScript common pattern shortcut definitions / utility gist library
npm install tsdef

!npm type definitions



npm install -D tsdef
`
or
`
yarn add -D tsdef
`How to Use
`typescript
import { Nullable, NonNull } from 'tsdef';const nullableString: Nullable = null; // ok
const nonNullString: NonNull = null; // error
`Documentation (Source Code)
`typescript
export type nil = null | undefined;export type Nilable = T | nil;
export type Nullable = T | null;
export type Undefinable = T | undefined;
export type MaybeNil = T | nil;
export type MaybeNull = T | null;
export type MaybeUndefined = T | undefined;
export type MaybePromise = T | Promise;
export type MaybeArray = T | T[];
export type MaybeAsReturnType = T | ((...args: any) => T);
// removes both null or undefined from T
export type NonNil = T extends nil ? never : T;
// removes null from T
export type NonNull = T extends null ? never : T;
// removes undefined from T
export type NonUndefined = T extends undefined ? never : T;
// make all properties nilable
export type NilableProps = { [P in keyof T]?: T[P] | nil };
// make all properties nullable
export type NullableProps = { [P in keyof T]: T[P] | null };
// make all properties undefinable
export type UndefinableProps = { [P in keyof T]?: T[P] | undefined };
// make all properties non nilable
export type NonNilProps = { [P in keyof T]-?: NonNil };
// make all properties non null
export type NonNullProps = { [P in keyof T]: NonNull };
// make all properties non undefined
export type NonUndefinedProps = { [P in keyof T]-?: NonUndefined };
// make all properties required
export type RequiredProps = { [P in keyof T]-?: T[P] };
// make all properties non readonly
export type WritableProps = { -readonly [P in keyof T]: T[P] };
// string | number | symbol
export type AnyKey = keyof any;
// matches any functions
export type AnyFunction = (...args: any) => any;
// matches any constructors
export type AnyConstructor = new (...args: any) => any;
// matches classes
export interface AnyClass {
prototype: any;
new (...args: any): any;
}
// matches prototypes
export interface AnyPrototype {
constructor: any;
}
// matches any objects
export interface AnyObject {
[key: string]: any;
[key: number]: any;
}
// matches objects with string keys
export interface AnyObjectWithStringKeys {
[key: string]: any;
}
// matches objects with number keys
export interface AnyObjectWithNumberKeys {
[key: number]: any;
}
// without some keys
export type ExcludeKeys = Omit;
// values of object
export type ValueOf = T[keyof T];
// get a property
export type Property = K extends keyof T ? T[K] : never;
// get keys with values of given type
export type KeyOfType = {
[P in keyof T]-?: T[P] extends U ? P : never
}[keyof T];
// get keys with values of given sub type
// For some reason, this works and KeyOfType doesn't when U = undefined
export type KeyOfSubType = {
[P in keyof T]-?: U extends T[P] ? P : never
}[keyof T];
// make some keys optional
export type WithOptionalKeys = Omit &
Partial>;
// make some keys nilable
export type WithNilableKeys = Omit &
NilableProps>;
// make some keys nullable
export type WithNullableKeys = Omit &
NullableProps>;
// make some keys undefinable
export type WithUndefinableKeys = Omit &
UndefinableProps>;
// make some keys non nil
export type WithNonNilKeys = Omit &
NonNilProps>;
// make some keys non null
export type WithNonNullKeys = Omit &
NonNullProps>;
// make some keys non undefined
export type WithNonUndefinedKeys = Omit &
NonUndefinedProps>;
// make all properties optional recursively including nested objects.
// keep in mind that this should be used on json / plain objects only.
// otherwise, it will make class methods optional as well.
export type DeepPartial = {
[P in keyof T]?: T[P] extends Array
? Array>
: DeepPartial
};
// first object properties excluding common keys with second object
export type DiffObjects = Omit;
// union of two objects
export type UnionObjects<
T extends AnyObject,
U extends AnyObject
> = DiffObjects &
{ [P in keyof T & keyof U]: T[P] | U[P] } &
DiffObjects;
// similar to Object.assign
export type OverwriteProps = U & DiffObjects;
// get arguments type
export type Arguments = Parameters;
// get arguments type
export type FirstArgument = T extends (
arg: infer A,
...args: any
) => any
? A
: never;
// get return value type
export type Return = ReturnType;
// get return type if a function, otherwise return itself
export type MaybeReturnType = T extends (...args: any[]) => infer R ? R : T;
// get instance type of class
export type InstanceOf = InstanceType;
// get promise return type
// PromisedType> = T
export type PromisedType> = T extends Promise
? R
: never;
// get promise return type if promise, otherwise return itself
// MaybePromisedType> = T
export type MaybePromisedType = T extends Promise ? R : T;
// get promise return type
export type MaybeAsyncReturnType = MaybePromisedType<
ReturnType
>;
// get array item type
export type ItemType = T extends Array ? I : never;
// get thunk for type
export type Thunk = () => T;
// get thunk or self
export type MaybeThunk = T | Thunk;
// get return type of thunk
export type Unthunk> = T extends Thunk
? R
: never;
// get return type if thunk, otherwise get self
export type MaybeUnthunk = T extends Thunk ? R : T;
// get inferred type of array item or return value or promised value
export type Unpack = T extends Array
? I
: T extends (...args: any) => infer R
? R
: T extends Promise
? P
: T;
// InheritClass: class C1 extends C2 {}
export type InheritClass = {
prototype: OverwriteProps;
new (...args: ConstructorParameters): OverwriteProps<
C2['prototype'],
C1['prototype']
>;
} & OverwriteProps;
// return Then if T is not null nor undefined, otherwise return False
// test null and undefined separately to prevent side effect from args distribution
export type IsNonNil = null extends T
? False
: undefined extends T
? False
: True;
// return True if T is not null, otherwise return False
export type IsNonNull = null extends T ? False : True;
// return True if T is not undefined, otherwise return False
export type IsNonUndefined = undefined extends T
? False
: True;
// return True if T is
never, otherwise return False
// wrap with array to prevent args distributing
export type IsNever = [T] extends [never]
? True
: False;// return True if T is
any, otherwise return False
export type IsAny = (
| True
| False) extends (T extends never ? True : False)
? True
: False;// return True if T is
unknown, otherwise return False
export type IsUnknown = unknown extends T
? IsAny
: False;// return True if T strictly includes U, otherwise return False
export type StrictlyIncludes = Exclude<
U,
T
> extends never
? (IsAny extends 1
? True
: (IsAny extends 1
? False
: (IsUnknown extends 1 ? IsUnknown : True)))
: False;
// tests and returns True if they are equal, False otherwise.
// wrap with array and do both ways to prevent args distrubition
export type AreStrictlyEqual = StrictlyIncludes<
T,
U,
1,
0
> extends 1
? StrictlyIncludes
: False;
// tests and returns True if both objects have same keys, False otherwise.
export type HaveSameKeys = (
| Exclude
| Exclude) extends never
? True
: False;
// https://github.com/Microsoft/TypeScript/issues/26051
export type Exact = T &
{ [K in keyof X]: K extends keyof T ? X[K] : never };
// U extends Exact<{ [K in keyof T]-?: (v: T[K]) => any }, U>
``