Skip to content

Latest commit

 

History

History
613 lines (412 loc) · 8.8 KB

File metadata and controls

613 lines (412 loc) · 8.8 KB

typelab / aliases

Handy TypeScript type aliases for everyday things like arrays, objects, functions, and promises. It also includes special cases like primitive, nullable, nullish, and undefinable types.

Type Aliases

Type Alias Description

Any

Type that refers to any.

Useful for bypass lint or type coverage checks. Please use only in circumstances that require it.

See *this link*.

Remarks

Basically the same as TypeScript's built-in any.

It's better to not use any.

Anything

Type that refers to any.

Useful for bypass lint or type coverage checks. Please use only in circumstances that require it.

See *this link*.

Remarks

Basically the same as TypeScript's built-in any.

It's better to not use any.

Array

Type that represents an array with elements of type T.

Example

// MyArray is string[]
type MyArray = Array<string>;

Arr

Alias for `Array`.

List

Alias for `Array`.

WritableArray

Alias for `Array`.

WritableArr

Alias for `Array`.

WritableList

Alias for `Array`.

ReadonlyArray

Type that represents a read-only array with elements of type T.

Example

// MyReadonlyArray is readonly string[]
type MyReadonlyArray = ReadonlyArray<string>;

ReadonlyArr

Alias for `ReadonlyArray`.

ReadonlyList

Alias for `ReadonlyArray`.

Tuple

Type that represents a tuple, which is a fixed-length array of elements of type T.

Example

// MyTuple is [number, string]
type MyTuple = Tuple<[number, string]>;

WritableTuple

Alias for `Tuple`.

ReadonlyTuple

Type that represents a read-only tuple, which is a fixed-length, immutable array of elements of type T.

Example

// MyReadonlyTuple is readonly [number, string]
type MyReadonlyTuple = ReadonlyTuple<[number, string]>;

Class

Type that represents a class constructor that takes specified parameters types and returns an instance of a specified type.

Example

// C is a class
// with constructor(id: number, name: string)
// and {id: number, name: string} as instance and prototype
type C = Class<[id: number, name: string], {id: number, name: string}>;

Falsy

Type that represents falsy value.

This includes false, '', 0, 0n, null, undefined, and void.

Function

Type that represents a function that takes specified parameters types and returns a specified type.

Example

// MyFunction is (param_0: number, param_1: string) => string
type MyFunction = Function<[number, string], string>;

Fn

Alias for `Function`.

AsyncFunction

Type that represents an async function that takes specified parameters types and returns a specified type.

Example

// MyAsyncFunction is (param_0: number, param_1: string) => Promise<string>
type MyAsyncFunction = AsyncFunction<[number, string], string>;

AsyncFn

Alias for `AsyncFunction`.

NewableFunction

Type that represents a newable function that takes specified parameters types and returns a specified type.

Example

// MyNewableFunction is new (param_0: number, param_1: string) => string`
type MyNewableFunction = NewableFunction<[number, string], string>;

NewableFn

Alias for `NewableFn`.

PositiveInfinity

Represents positive infinity in TypeScript as a constant value.

Not a real type for javascript built-in Infinity or Number.POSITIVE_INFINITY.

NegativeInfinity

Represents negative infinity in TypeScript as a constant value.

Not a real type for javascript built-in -Infinity or Number.NEGATIVE_INFINITY.

Infinity

Represents positive or negative infinity in TypeScript as a constant value.

Not a real type for javascript built-in Infinity or Number.POSITIVE_INFINITY.

Not a real type for javascript built-in -Infinity or Number.NEGATIVE_INFINITY.

NonNullish

Type that refers to any (non-null/undefined) value with zero or more properties.

See this link.

Example

const example: NonNullish = true; // valid
const example2: NonNullish = true as boolean | null; // invalid
const example3: NonNullish = null || undefined; // invalid

Nullable

Type that allows a value to be T or null.

Example

const example: Nullable<string> = null; // valid
const example2: Nullable<string> = undefined; // invalid

Nullish

Type that allows a value to be T, null, or undefined.

Example

const example: Nullish = null; // valid
const example2: Nullish = undefined; // valid

ObjectGeneric

Extended TypeScript `Record` to define a generic object type.

Example

// ObjNumber is { [x: string]: number; [x: number]: number; [x: symbol]: number; }
type ObjNumber = ObjectGeneric<number>;

const Valid: ObjNumber = { a: 1, b: 2 };
const Invalid: ObjNumber = { a: '1', b: '2' }; // Type 'string' is not assignable to type 'number'

ObjectEmpty

Extended TypeScript `Record` to define a empty object type.

Example

// ObjEmpty is { [x: string]: never; [x: number]: never; [x: symbol]: never; }
type ObjEmpty = ObjectEmpty;

const Valid: ObjEmpty = {};
const Invalid: ObjEmpty = { a: 1 } // Type 'number' is not assignable to type 'never'

ObjectArrayLike

Extended TypeScript `Record` to define an object with number as key type.

Example

// ObjArrayLike is { [x: number]: string; }
type ObjArrayLike = ObjectArrayLike<string>;

const Valid: ObjArrayLike = { 0: 'first', 1: 'second' };
const Invalid: ObjArrayLike = { 0: 'first', a: 'second' }; // a is invalid key

Primitive

Type that represents any primitive value.

This includes boolean, string, number, bigint, symbol, null, and undefined.

Example

const value1: Primitive = ""; // valid
const value2: Primitive = {}; // invalid

Async

Built-in `Promise` with default type.

Example

// AsyncValue is Promise<string>
type AsyncValue = Async<string>;

Undefinable

Type that allows a value to be T or undefined.

Example

const example: Undefinable<string> = undefined; // valid
const example2: Undefinable<string> = null; // invalid

Void

Type that refers to void.

Useful for bypass lint.

Remarks

Basically the same as TypeScript's built-in void.

It's better to not use void outside a return type.