Skip to main content Link Search Menu Expand Document (external link)

types overview

A simple type module


Table of contents


Constants

contravariantValue

A value with a contravariant type

Signature

export declare const contravariantValue: (_: unknown) => unknown

covariantValue

A value with a covariant type

Signature

export declare const covariantValue: (_: never) => never

invariantValue

A value with an invariant type

Signature

export declare const invariantValue: (_: any) => any

Guards

isArray

From unknown to Array. Not based on Array.isArray from a Typescript perspective because it is bugged. See https://github.com/microsoft/TypeScript/issues/17002

Signature

export declare const isArray: <T>(arg: T) => arg is ArrayType<T>

isBigInt

From unknown to bigint

Signature

export declare const isBigInt: (input: unknown) => input is bigint

isBoolean

From unknown to boolean

Signature

export declare const isBoolean: (input: unknown) => input is boolean

isEmptyArray

From Array<A> to EmptyArray

Signature

export declare const isEmptyArray: <A>(u: Array<A>) => u is EmptyArray

isEmptyReadonlyArray

From ReadonlyArray<A> to EmptyReadonlyArray

Signature

export declare const isEmptyReadonlyArray: <A>(u: ReadonlyArray<A>) => u is EmptyReadonlyArray

isErrorish

From unknown to Errorish

Signature

export declare const isErrorish: (u: unknown) => u is Errorish

isFunction

From unknown to AnyFunction

Signature

export declare const isFunction: (u: unknown) => u is AnyFunction

isIterable

From unknown to Iterable<unknown>. DOES NOT WORK FOR string which is the only primitive type to be iterable

Signature

export declare const isIterable: (input: unknown) => input is Iterable<unknown>

isNonPrimitive

From unknown to NonPrimitive

Signature

export declare const isNonPrimitive: <A>(input: A) => input is Exclude<A, Primitive> & NonPrimitive

isNotNull

From a type T to the same type T without null

Signature

export declare const isNotNull: <A>(input: A) => input is Exclude<A, null>

isNotNullable

From a type T to the same type T without null and undefined

Signature

export declare const isNotNullable: <A>(input: A) => input is NonNullable<A>

isNotUndefined

From a type T to the same type T without undefined

Signature

export declare const isNotUndefined: <A>(input: A) => input is Exclude<A, undefined>

isNull

From unknown to null

Signature

export declare const isNull: (input: unknown) => input is null

isNullable

From a type T to null or undefined depending on what is in T

Signature

export declare const isNullable: <A>(input: A) => input is Extract<A, null | undefined>

isNumber

From unknown to number

Signature

export declare const isNumber: (input: unknown) => input is number

isOneArgFunction

From a function with many arguments to a function with a single argument

Signature

export declare const isOneArgFunction: <A, R>(f: (a: A, ...args: ReadonlyArray<any>) => R) => f is (a: A) => R

isOverOne

From Array<A> to OverOne<A>

Signature

export declare const isOverOne: <A>(u: Array<A>) => u is OverOne<A>

isOverTwo

From Array<A> to OverTwo<A>

Signature

export declare const isOverTwo: <A>(u: Array<A>) => u is OverTwo<A>

isPair

From Array<A> to Pair<A,A>

Signature

export declare const isPair: <A>(u: Array<A>) => u is Pair<A, A>

isPrimitive

From unknown to Primitive

Signature

export declare const isPrimitive: <A>(input: A) => input is Exclude<A, NonPrimitive> & Primitive

isReadonlyOverOne

From ReadonlyArray<A> to ReadonlyOverOne<A>

Signature

export declare const isReadonlyOverOne: <A>(u: ReadonlyArray<A>) => u is ReadonlyOverOne<A>

isReadonlyOverTwo

From ReadonlyArray<A> to ReadonlyOverTwo<A>

Signature

export declare const isReadonlyOverTwo: <A>(u: ReadonlyArray<A>) => u is ReadonlyOverTwo<A>

isReadonlyPair

From ReadonlyArray<A> to ReadonlPair<A>

Signature

export declare const isReadonlyPair: <A>(u: ReadonlyArray<A>) => u is ReadonlyPair<A, A>

isReadonlySingleton

From ReadonlyArray<A> to ReadonlySingleton<A>

Signature

export declare const isReadonlySingleton: <A>(u: ReadonlyArray<A>) => u is ReadonlySingleton<A>

isSingleton

From Array<A> to Singleton<A>

Signature

export declare const isSingleton: <A>(u: Array<A>) => u is Singleton<A>

isString

From unknown to string

Signature

export declare const isString: (input: unknown) => input is string

isSymbol

From unknown to symbol

Signature

export declare const isSymbol: (input: unknown) => input is symbol

isTypedArray

From unknown to TypedArray

Signature

export declare const isTypedArray: <A>(input: A) => input is Extract<A, TypedArray>

isUndefined

From unknown to undefined

Signature

export declare const isUndefined: (input: unknown) => input is undefined

Information

typedArrayName

If u is a TypedArray, returns a some of its name (e.g. UInt8Array). Otherwise, returns a none

Signature

export declare const typedArrayName: (u: unknown) => Option.Option<string>

Models

AnyArray (interface)

Type that represents an array

Signature

export interface AnyArray extends Array<any> {}

AnyFunction (interface)

Type that represents a function

Signature

export interface AnyFunction {
  (...args: ReadonlyArray<any>): any
}

AnyPredicate (type alias)

Type that represents any predicate or refinement

Signature

export type AnyPredicate = Predicate.Predicate<any>

AnyReadonlyArray (interface)

Type that represents a ReadonlyArray

Signature

export interface AnyReadonlyArray extends ReadonlyArray<any> {}

AnyRefinement (type alias)

Type that represents any refinement

Signature

export type AnyRefinement = Predicate.Refinement<any, any>

Category (namespace)

Namespace for the possible categories of a Javascript value

EmptyArray (type alias)

Type that represents an empty array or tuple

Signature

export type EmptyArray = []

EmptyReadonlyArray (type alias)

Type that represents an empty array or tuple

Signature

export type EmptyReadonlyArray = readonly []

Errorish (type alias)

Type that represents a value that can be used as an error

Signature

export type Errorish = { readonly message: string; readonly stack?: string | undefined }

MutableArray (interface)

Type used to avoid warnings by eslint/functional when functions return a mutable array

Signature

export interface MutableArray<T> extends Array<T> {}

NonNullablePrimitive (type alias)

Type that represents a primitive except null and undefined

Signature

export type NonNullablePrimitive = string | number | bigint | boolean | symbol

NonPrimitive (interface)

Type that represents a non-null object as defined in javascript. It includes records (in their usual computer science meaning), class instances, arrays, and functions but not null or undefined.

Signature

export interface NonPrimitive {
  readonly [key: string | symbol]: any
}

OneArgFunction (interface)

Type that represents a function with one argument

Signature

export interface OneArgFunction<in A, out B = A> {
  (a: A): B
}

OverOne (type alias)

Type that represents a non empty array

Signature

export type OverOne<A> = [A, ...Array<A>]

OverTwo (type alias)

Type that represents an array with at least two elements

Signature

export type OverTwo<A> = [A, A, ...Array<A>]

Pair (type alias)

Type that represents a tuple or array with two elements

Signature

export type Pair<A, B> = [A, B]

Primitive (type alias)

Type that represents a primitive

Signature

export type Primitive = NonNullablePrimitive | null | undefined

ReadonlyOverOne (type alias)

Type that represents a non empty array

Signature

export type ReadonlyOverOne<A> = readonly [A, ...ReadonlyArray<A>]

ReadonlyOverTwo (type alias)

Type that represents an array with at least two elements

Signature

export type ReadonlyOverTwo<A> = readonly [A, A, ...ReadonlyArray<A>]

ReadonlyPair (type alias)

Type that represents a tuple or array with two elements

Signature

export type ReadonlyPair<A, B> = readonly [A, B]

ReadonlySingleton (type alias)

Type that represents a tuple or array with one element

Signature

export type ReadonlySingleton<A> = readonly [A]

RefinementFrom (type alias)

Type that represents any refinement from a given type

Signature

export type RefinementFrom<Source> = Predicate.Refinement<Source, any>

Singleton (type alias)

Type that represents a tuple or array with one element

Signature

export type Singleton<A> = [A]

StringTransformer (interface)

Type of a string transformer, i.e. a function that transforms a string into another one

Signature

export interface StringTransformer extends OneArgFunction<string> {}

TypedArray (type alias)

Type that represents all typed arrays

Signature

export type TypedArray = toTypedArrayInstances<_allTypedArrayConstructorsType>[number]

Unknown (type alias)

Type that represents all possible Javascript values but not all possible Typescript types (it does not represent Branded types for instance)

Signature

export type Unknown = Primitive | NonPrimitive

Utility types

Data (type alias)

Utility type that removes all non-data from a type.

Signature

export type Data<T extends NonPrimitive, ProtoFunctions extends string | symbol = never> = {
  readonly [k in keyof T as readonly [k] extends readonly [BaseProtoKeys | ProtoFunctions] ? never : k]: T[k]
}

Equals (type alias)

Utility type that retuns never if two types are equal. unknown otherwise

Signature

export type Equals<A, B> = readonly [A] extends readonly [B]
  ? readonly [B] extends readonly [A]
    ? never
    : unknown
  : unknown

IntRange (type alias)

Utility type that generates a range of numeric literal types

Signature

export type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>

IntersectAndSimplify (type alias)

Utility type that creates an intersection and simplifies it which Typescript does not do by itself (see https://stackoverflow.com/questions/72395823/why-does-typescript-not-simplify-the-intersection-of-a-type-and-one-of-its-super)

Signature

export type IntersectAndSimplify<T, U> = readonly [T] extends readonly [U]
  ? T
  : readonly [U] extends readonly [T]
    ? U
    : T & U

MapToReadonlyTarget (type alias)

Utility type that changes the types of all keys of a tuple, array, struct or record to Target

Signature

export type MapToReadonlyTarget<Tuple, Target> = {
  readonly [k in keyof Tuple]: Target
}

Proto (type alias)

Utility type that removes all data from a type

Signature

export type Proto<T extends NonPrimitive, ProtoFunctions extends string | symbol = never> = Omit<
  T,
  keyof Data<T, ProtoFunctions>
>

ReadonlyTail (type alias)

Utility type that extracts all elements of a tuple but the first

Signature

export type ReadonlyTail<T> = readonly [T] extends readonly [readonly [any, ...infer R]]
  ? { readonly [key in keyof R]: R[key] }
  : never

SetArgTypeTo (type alias)

Utility type that changes the type of the unique parameter of F to A if F is a Refinement or a OneArgFunction. Returns F otherwise

Signature

export type SetArgTypeTo<F, A> =
  F extends Predicate.Refinement<infer _, infer R>
    ? R extends A
      ? Predicate.Refinement<A, R>
      : F
    : F extends OneArgFunction<any, infer R>
      ? OneArgFunction<A, R>
      : F

ToKeyIntersection (type alias)

Utility type that creates an intersection of all keys of a type. Meant to be used with Tuples even though not set as a constraint

Signature

export type ToKeyIntersection<T> = {
  readonly [K in keyof T]: (x: T[K]) => void
} extends {
  readonly [K: number]: (x: infer I) => void
}
  ? I
  : never

Utils

checkNever

Function that reports an error if the type it receives is not never

Signature

export declare function checkNever<_A extends never>(): void

objectFromDataAndProto

Constructs an object with prototype proto and data data

Signature

export declare const objectFromDataAndProto: <P extends NonPrimitive, D extends NonPrimitive>(
  proto: P,
  data: D
) => P & D