@skypilot/common-types

Library of reusable common types for TypeScript projects

Usage no npm install needed!

<script type="module">
  import skypilotCommonTypes from 'https://cdn.skypack.dev/@skypilot/common-types';
</script>

README

@skypilot/common-types

npm stable stable build npm next next build Codacy grade downloads license: ISC

A library of reusable common types for TypeScript projects

Exports

Convenience aliases

type AnyRecord = Record<string, any>

// Describes an object without properties
type EmptyObject = { [key: string]: never; [key: number]: never }

Emulation types

interface Class<T> extends Function {
  new(...args: any[]): T
}

type Enum<E> = Record<keyof E, number | string> & { [key: number]: string }

type Primitive = boolean | number | string;

JSON types

// Constructs a type that matches any serializable value
export type AsJsonValue<T> =
  T extends JsonPrimitive ? T :
    T extends Function ? never :
      T extends Array<unknown> ? AsJsonArray<T> :
        T extends object ? AsJsonMap<T> :
          never;

//  Constructs a type that matches any serializable array
export type AsJsonArray<T> = Array<AsJsonValue<UnpackedArray<T>>>

// Constructs a type that matches any serializable object map
export type AsJsonMap<T> =
  T extends Array<unknown> ? never :
    {[K in keyof T]: AsJsonValue<T[K]> };

interface DefiniteJsonArray extends Array<DefiniteJsonValue> {}

type DefiniteJsonMap = {
  [key: string]: DefiniteJsonArray | DefiniteJsonMap | JsonPrimitive;
}

type DefiniteJsonValue = DefiniteJsonArray | DefiniteJsonMap | JsonPrimitive;

interface JsonArray extends Array<JsonValue> {}

type JsonMap = {
  [key: string]: JsonArray | JsonMap | JsonPrimitive | undefined;
}

type JsonPrimitive = boolean | number | string | null;

type JsonValue = JsonArray | JsonMap | JsonPrimitive | undefined;

Pseudo-types

These types provide semantic value only. (Until TypeScript allows pattern-matching, the patterns these types imply will not be enforced.)

type Email = string

type Integer = number

type Json = string

type Timestamp = Integer

type Url = string

Utility types

ArrayItems<A>: constructs a type that is the union of all items in array A

AtLeastOne<T>: constructs a type that requires at least one property of T

ExactlyOne<T>: constructs a type that requires exactly one property of T

Maybe<T>: constructs a type that allows T, null, or undefined

MaybeNull<T>: constructs a type that allows T or null

MaybeReadOnly<T>: constructs a type that allows T[] or readonly T[]

MaybeUndefined<T>: constructs a type that allows T or undefined

ObjectEntries<O>: constructs a type that is the union of the types of all entries in object O

ObjectKeys<O>: constructs a type that is the union of all keys in object O

ObjectValues<O, K?>: constructs a type that is the union of all values in object O, or, if K is provided, of all values mapped to keys K in object O

PromisedReturnType<F>: obtains the promised return type of function F

Require<T, Required> [DEPRECATED in favor of RequireProps]: constructs a type that makes the Required properties of T required

RequireProps<T, Required>: constructs a type that makes the Required properties of T required

UnpackedArray<T>: constructs a type that is the union of the types of the items in an array of type T