Skip to content

Latest commit

 

History

History
1216 lines (835 loc) · 16.3 KB

File metadata and controls

1216 lines (835 loc) · 16.3 KB

typelab / assertions

Handy TypeScript type checks for figuring out what a type is. Includes helpers for things like arrays, objects, functions, primitives, and special cases like null, undefined, union types, and more.

Type Aliases

Type Alias Description

IsAny

Checks if a given type T is an any type.

Example

type Valid = IsAny<any>; // true
type Invalid = IsAny<string>; // false

IsArray

Checks if a given type T is an Array type.

Example

type Valid = IsArray<string[]>; // true
type Invalid = IsArray<string>; // false

IsArrayWritable

Checks if a given type T is not readonly Array type.

Example

type Valid = IsArrayWritable<string[]>; // true
type Invalid = IsArrayWritable<readonly string[]>; // false

IsArrayReadonly

Checks if a given type T is readonly Array type.

Example

type Valid = IsArrayReadonly<readonly string[]>; // true
type Invalid = IsArrayReadonly<string[]>; // false

IsTuple

Checks if a given type T is a Tuple type.

Example

type Valid = IsTuple<[string]>; // true
type Invalid = IsTuple<string[]>; // false

IsTupleWritable

Checks if a given type T is not readonly Tuple type.

Example

type Valid = IsTupleWritable<[string]>; // true
type Invalid1 = IsTupleWritable<readonly [string]>; // false
type Invalid2 = IsTupleWritable<string[]>; // false

IsTupleReadonly

Checks if a given type T is readonly Tuple type.

Example

type Valid = IsTupleReadonly<readonly [string]>; // true
type Invalid1 = IsTupleReadonly<[string]>; // false
type Invalid2 = IsTupleReadonly<readonly string[]>; // false

IsBigInt

Checks if a given type T is a bigint.

Example

type Valid = IsBigInt<bigint>; // true
type Invalid = IsBigInt<number>; // false

IsBigIntLiteral

Checks if a given type T is a BigInt Literal type.

Example

type Valid = IsBigIntLiteral<1n>; // true
type Invalid = IsBigIntLiteral<bigint>; // false

IsBoolean

Checks if a given type T is a boolean.

Example

type Valid = IsBoolean<boolean>; // true
type Invalid = IsBoolean<string>; // false

IsFalse

Checks if a given type T is the literal false.

Example

type Valid = IsFalse<false>; // true
type Invalid = IsFalse<boolean>; // false

IsFalsy

Checks if a given type T is `Falsy`.

Example

type Valid = IsFalsy<false | '' | 0 | 0n | null | undefined | void>; // true
type Invalid = IsFalsy<0 | 1>; // false

IsTrue

Checks if a given type T is the literal true.

Example

type Valid = IsTrue<true>;    // true
type Invalid = IsTrue<boolean>; // false

IsTruthy

Checks if a given type T is not `Falsy` or never.

Example

type Valid = IsTruthy<0 | 1>; // true
type Invalid = IsTruthy<0 | 1>; // false

IsClass

Checks if a given type T is a Class type.

Example

class DummyClass {}
type Valid = IsClass<typeof DummyClass>; // true
type Invalid = IsClass<string>; // false

IsEqual

Check the type equality between T1 and T2.

Example

type Equal = IsEqual<string, string>; // true
type NotEqual = IsEqual<string, ''>; // false

IsNotEqual

Check the type inequality between T1 and T2.

`IsEqual` in reverse.

Example

type NotEqual = IsNotEqual<string, ''>; // true
type Equal = IsNotEqual<string, string>; // false

IsExtends

Type that checks if type T1 extends type T2.

Example

type Extends = IsExtends<'', string>; // true
type NotExtends = IsExtends<string, ''>; // false

IsNotExtends

Type that checks if type T1 not extends type T2.

`IsExtends` in reverse.

Example

type NotExtends = IsNotExtends<string, ''>; // true
type Extends = IsNotExtends<'', string>; // false

IsAssignable

Alias for `IsExtends`.

IsNotAssignable

Alias for `IsNotExtends`.

IsExtendsMutually

Type that checks if type T1 extends type T2 and type T2 extends type T1.

Example

type Mutual = IsExtendsMutually<string, any>; // true
type NotMutual = IsExtendsMutually<string, ''>; // false

IsNotExtendsMutually

Type that checks if type T1 not extends type T2 or type T2 not extends type T1.

Example

type NotMutual = IsNotExtendsMutually<string, ''>; // true
type Mutual = IsNotExtendsMutually<string, string>; // false

IsAssignableMutually

Alias for `IsExtendsMutually`.

IsNotAssignableMutually

Alias for `IsNotExtendsMutually`.

IsExtendsEitherWay

Type that checks if type T1 extends type T2 or type T2 extends type T1.

Example

type Relate = IsExtendsEitherWay<string, ''>; // true
type NotRelate = IsExtendsEitherWay<string, number>; // false

IsNotExtendsEitherWay

Type that checks if type T1 not extends type T2 and type T2 not extends type T1.

Example

type NotRelate = IsNotExtendsEitherWay<string, number>; // false
type Relate = IsNotExtendsEitherWay<string, ''>; // true

IsAssignableEitherWay

Alias for `IsExtendsEitherWay`.

IsNotAssignableEitherWay

Alias for `IsNotExtendsEitherWay`.

IsFunction

Checks if a given type T is a function type.

Example

type Valid = IsFunction<() => string>; // true
type Invalid = IsFunction<string>; // false

IsFn

Alias for `IsFunction`.

IsAsyncFunction

Checks if a given type T is an async function type.

Example

type Valid = IsAsyncFunction<() => Promise<string>>; // true
type Invalid = IsAsyncFunction<() => string>; // false

IsAsyncFn

Alias for `IsAsyncFunction`.

IsNewableFunction

Checks if a given type T is a newable function type.

Example

type Valid = IsNewableFunction<new () => string>; // true
type Invalid = IsNewableFunction<() => string>; // false

IsNewableFn

Alias for `IsNewableFunction`.

IsIntersected

Checks if type T1 is intersected with type T2.

Example

type Intersected = IsIntersected<'a' | 'c', 'a' | 'b'>; // true
type NotIntersected = IsIntersected<'c', 'a' | 'b'>; // false

IsNotIntersected

Checks if type T1 is not intersected with type T2.

Example

type Intersected = IsIntersected<'a' | 'c', 'a' | 'b'>; // true
type NotIntersected = IsIntersected<'c', 'a' | 'b'>; // false

IsNegative

Checks if a given number T is negative.

Example

type Valid = IsNegative<-11 | -11n>; // true
type Invalid = IsNegative<11 | 11n>; // false

IsNever

Checks if a given type T is a never type.

Example

type Valid = IsNever<never>; // true
type Invalid = IsNever<string>; // false

IsNonNullish

Checks if a given type T is a {} type.

Example

type Valid = IsNonNullish<{}>; // true
type Invalid = IsNonNullish<string>; // false

IsNull

Checks if a given type T is a null type.

Example

type Valid = IsNull<{}>; // true
type Invalid = IsNull<string>; // false

IsNullable

Checks if a given type T has null type.

Example

type Valid = IsNullable<string | null>; // true
type Invalid = IsNullable<string>; // false

IsNullish

Checks if a given type T has null | undefined type.

Example

type Valid1 = IsNullish<string | null>; // true
type Valid2 = IsNullish<string | undefined>; // true
type Invalid = IsNullish<string>; // false

IsNumber

Checks if a given type T is a number type.

Example

type Valid = IsNumber<number>; // true
type Invalid = IsNumber<string>; // false

IsNumberLiteral

Checks if a given type T is a Number Literal type.

Example

type Valid = IsNumberLiteral<11>; // true
type Invalid = IsNumberLiteral<number>; // false

IsNumberDecimal

Checks if a given type T is a decimal number literal type.

Example

type Valid = IsNumberDecimal<11.1>; // true
type Invalid = IsNumberDecimal<11>; // false

IsNumberInteger

Checks if a given type T is a integer number literal type.

Example

type Valid = IsNumberInteger<11>; // true
type Invalid = IsNumberInteger<11.1>; // false

IsObject

Checks if a given type T is an object type.

Example

type Valid = IsObject<[]>; // true
type Invalid = IsObject<string>; // false

IsObjectLiteral

Checks if a given type T is an ObjectLiteral type.

Example

type Valid = IsObjectLiteral<{ a: number; b: string }>; // true
type Invalid = IsObjectLiteral<string[]>; // false

IsObjectEmpty

Checks if a given type T is an Object Empty type.

Example

type Valid = IsObjectEmpty<{ [x: string]: never }>; // true
type Invalid = IsObjectEmpty<{ a: string }>; // false

IsObjectArrayLike

Checks if a given type T is an Object Array Like type.

Example

type Valid = IsObjectArrayLike<{ 0: number; 1: number }>; // true
type Invalid = IsObjectArrayLike<{ 0: number; a: number }>; // false

IsOptionalProperty

Determines whether the type of T[Key] is optional.

Example

type Valid = IsOptionalProperty<{ a?: string }, 'a'>; // true
type Invalid = IsOptionalProperty<{ a: string }, 'a'>; // false
type Never1 = IsOptionalProperty<{ a: string }, 'b'>; // never
type Never2 = IsOptionalProperty<{}, 'b'>; // never
type Never3 = IsOptionalProperty<undefined, 'b'>; // never

IsPositive

Checks if a given number T is positive.

Example

type Valid = IsPositive<11 | 11n>; // true
type Invalid = IsPositive<-11 | -11n>; // false

IsPrimitive

Checks if a given type T is a Primitive type.

Example

type Valid = IsPrimitive<string>; // true
type Invalid = IsPrimitive<object>; // false

IsPromise

Checks if a given type T is a Promise type.

Example

type Valid = IsPromise<Promise<string>>; // true
type Invalid = IsPromise<string>; // false

IsAsync

Alias for `IsPromise`.

IsPropertyKey

Checks if a given type T is a `PropertyKey` type.

Example

type Valid = IsPropertyKey<string>; // true
type Invalid = IsPropertyKey<bigint>; // false

IsRequiredProperty

Determines whether the type of T[Key] is required.

Example

type Valid = IsRequiredProperty<{ a: string }, 'a'>; // true
type Invalid = IsRequiredProperty<{ a?: string }, 'a'>; // false
type Never1 = IsRequiredProperty<{ a: string }, 'b'>; // never
type Never2 = IsRequiredProperty<{}, 'b'>; // never
type Never3 = IsRequiredProperty<undefined, 'b'>; // never

IsString

Checks if a given type T is a string.

Example

type Valid = IsString<string>; // true
type Invalid = IsString<number>; // false

IsStringLiteral

Checks if a given type T is a String Literal type.

Example

type Valid = IsStringLiteral<'str'>; // true
type Invalid = IsStringLiteral<string>; // false

IsSymbol

Checks if a given type T is a symbol.

Example

type Valid = IsSymbol<symbol>; // true
type Invalid = IsSymbol<string>; // false

IsUndefinable

Checks if a given type T has undefined type.

Example

type Valid = IsUndefinable<string | undefined>; // true
type Invalid = IsUndefinable<string>; // false

IsUndefined

Checks if a given type T is undefined.

Example

type Valid = IsUndefined<undefined>; // true
type Invalid = IsUndefined<string>; // false

IsUnion

Checks if a given type T is union type.

Example

type Valid = IsUnion<string | number>; // true
type Invalid = IsUnion<string>; // false

IsUnknown

Checks if a given type T is an unknown type.

Example

type Valid = IsUnknown<unknown>; // true
type Invalid = IsUnknown<string>; // false

IsVoid

Checks if a given type T is void.

Example

type Valid = IsVoid<void>; // true
type Invalid = IsVoid<string>; // false