Options
All
  • Public
  • Public/Protected
  • All
Menu

Common interfaces and types for typescript, such Predicate or Maybe. This is of no use if you do not use typescript. I made this because I often ended up needing and writing the same types for my projects.

Types are used only when transpiling, they do not end up in the transpiled code. Ie, your transpiled code does not get bigger by using this library.

Documentation

Documentation and list of all the interfaces and types.

Install

The drill:

npm install --save andross

Now you can import the types

Usage

All available and types are exposed from the main file.

// import this lib
import { Predicate, Maybe, Comparator } from "andross";

// Use the interfaces and type.
class Users<T> {
  private users: Users[];
  constructor() {
    this.users = [];    
  }
  getUserById(id: number): Maybe<User> {
    return this.users.find(user => user.id === id);
  }npx tslint -c tslint.json main.ts
  getUsers(filter: Predicate<User>): User[] {
    return this.users.filter(filter);
  }
  sort(comparator: Comparator<T>) {
    this.users.sort(comparator);
  }
}

Enums

As of version 0.3.4, this package also includes some predefined enumerations. Since these are not purely type definitions and contribute to the size of a bundle, they are placed in a separate file and not included in the main file:

import { CardinalDirection4 } from "andross/enum";
const north = CardinalDirection4.North;

See the documentation for a list of all interfaces and types with a short description.

Changes

  • 0.3.10 Added some types for discriminated (tagged) unions.
  • 0.3.9 Added types several types for extracting keys from objects, such as PickPartial, PickMatching, PickAssignable, PartialKeys, AssignableKeys, RequiredFor.
  • 0.3.8 Added type Voidable.
  • 0.3.7 Added types ReversibleFunction, ReversibleBiFunction, ReversibleTriFunction.
  • 0.3.6 Added type Equatable and added defaults for type parameters of BiConsumer/TriConsumer and BiSupplier/TriSupplier.
  • 0.3.5 Added an optional type parameter K to StringObject, allowing you to restrict the available keys. Also removed the *.ts source files from the NPM package. The source file made webpack with ts-loader work incorrectly.
  • 0.3.4 Added types RectSize, Rectangle, MinMaxRectangle, CardinalDirection4/8/16/32, ReadonlyFor, ReadonlyExcept, MatchingKeys.
  • 0.3.3 Added types PartialExcept (makes every property but the given optional) and PartialFor (makes every given property optional).
  • 0.3.2 Added RemoveFrom. Tuple types generics now default to the previous type, ie. Pair<string> is equivalent to Pair<string, string>.
  • 0.3.1 Added Builder type.
  • 0.3.0 Added Vector types (Vector1, Vector2, Vector3, Vector4, Vector5), StringObject and NumberObject, as well as Omit and Overwrite
  • 0.2.0 Added JSON types.

Build

May not work on Windows.

git clone https://github.com/blutorange/js-andross
cd js-andross
npm install
npm run build

Teh name

The great (inter-)face of Andross.

Index

Type aliases

AssignableKeys

AssignableKeys: AssignableKeys<TRecord, TMatch, K>

Gives all property keys to which the given type can be assigned.

interface User {
  age: string | number;
  email: string | undefined;
  active?: boolean;
}

// A string can be assigned to the properties age and email.
type userString = AssignableKeys<User, string>; // "age"|"email"

// undefined can be assigned only to the properties email and active.
type userUndefined = AssignableKeys<User, string>; // "email"|"active"

BiConsumer

BiConsumer: function

Same as Consumer, but accepts two items to be consumed.

see

Consumer

Type declaration

    • (item1: T1, item2: T2): void
    • Parameters

      • item1: T1
      • item2: T2

      Returns void

BiPredicate

BiPredicate: function

Same as Predicate, but accepts two parameters.

Type declaration

    • (item1: T1, item2: T2): boolean
    • Parameters

      • item1: T1
      • item2: T2

      Returns boolean

BiSupplier

BiSupplier: function

Same as a Supplier, but returns two items.

see

Supplier

Type declaration

BinaryOperator

BinaryOperator: TypedBiFunction<T, T, T>

A binary operator takes two items of the same type and coputes a result of the same type.

const multiply: BinaryOperator<number> = (x, y) => x * y;
[1,2,3,4,5].reduce(multiply, 1); // => 120
typeparam

of the domain on which the operator operates.

CardinalDirection16

CardinalDirection16: CardinalDirection8 | "NorthNortheast" | "EastNortheast" | "EastSoutheast" | "SouthSoutheast" | "SouthSouthwest" | "WestSouthwest" | "WestNorthwest" | "NorthNorthwest"

List of the sixteen cardinal directions.

CardinalDirection32

CardinalDirection32: CardinalDirection16 | "NorthByEast" | "NortheastByNorth" | "NortheastByEast" | "EastByNorth" | "EastBySouth" | "SoutheastByEast" | "SoutheastBySouth" | "SouthByEast" | "SouthByWest" | "SouthwestBySouth" | "SouthwestByWest" | "WestBySouth" | "WestByNorth" | "NorthwestByWest" | "NorthwestByNorth" | "NorthByWest"

List of the thirty-two cardinal directions.

CardinalDirection4

CardinalDirection4: "North" | "East" | "South" | "West"

List of the four cardinal directions.

CardinalDirection8

CardinalDirection8: CardinalDirection4 | "Northeast" | "Southeast" | "Southwest" | "Northwest"

List of the eight cardinal directions.

Comparator

Comparator: function

A comparator that takes two objects and compares them. Returns a negative or positive number to indicate the first object is less or greater than the second object; or 0 iff both objects are equal.

const myComparator = (lhs, rhs) => rhs - lhs;
[3, 1, 2].sort(myComparator);
// => [3, 2, 1]
param

The first (left-hand side) object to compare.

param

The second (right-hand side) object to compare.

returns

A negative number iff lhs is strictly smaller than rhs, a positive number iff lhs is strictly greater than rhs; or 0 otherwise, when both objects are equal.

Type declaration

    • (lhs: T, rhs: T): number
    • Parameters

      • lhs: T
      • rhs: T

      Returns number

Constructor

Constructor: object

Represents the constructor a class, ie. the `constructor functions that returns a new instance of the class.

// Creates a new instance and injects all dependencies.
function create<T>(container: Constructor<T>, ...args: []) {
  const instance = new container(...args);
  // inject some properties
  return instance;
}

Type declaration

Consumer

Consumer: function

A consumer is a sink that takes an item and performs some action with it, but does not return anything.

function getViaAjax(endpoint: string, onDone: Consumer<object>) {
  fetch(endpoint)
     .then(response => JSON.parse(readBody(response)));
     .catch(e => console.error("Could not fetch data", e));
}

Type declaration

    • (item: T): void
    • Parameters

      • item: T

      Returns void

Decuple

Decuple: [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]

A 10-tuple with ten elements.

DeepPartial

DeepPartial: object

Consider an object with some known property keys. A partial is another object that may contain none or only some of these keys, but no keys not present in the original object. This is what the built-in type Partial provides. A deep partial generalize this notion to nested properties.

// Represents a physical address of a building etc.
class Address() {
  constructor(public country: string, public city: string, public street: string) {}
}

// Represents a user with an ID, a name, and a date of birth.
class User {
  constructor(public id: number, public name: string, residence: Address) {}
}

// A function that searches a user matching some criteria. By using a
// DeepPartial, `typescript` allows only keys and properties that are
// part of a User. It also checks whether the type of the property is
// correct.
function findUserBy(criteria: DeepPartial<User>) {
  // to be implemented, read from a database or in-memory
  return user;
}

// Now we can query users by their propeties.
findUserBy({
  id: 9,
});

findUserBy({
  name: "Masahiko",
  residence: {
    country: "Japan",
  }
});

findUserBy({
  residence: {
    city: "London",
    street: "Baker Street",
  }
});

Type declaration

DiscriminateUnion

DiscriminateUnion: DiscriminateUnion<T, K, V>

Takes a type and filter them, leaving only types that have a given property of a given type.

interface Square {
  kind: "square",
  geometry: {
    side: number;
  }
}

interface Circle {
  kind: "circle",
  geometry: {
    radius: number;
  }
}

interface Rectangle {
  kind: "rectangle",
  geometry: {
    horizontalSide: number;
    verticalSide: number;
  }
}

interface Ellipsis {
  kind: "ellipsis",
  geometry: {
    horizontalHalfAxis: number;
    verticalHalfAxis: number;
   }
}

// Union of all shapes
type Shape = Square | Circle | Rectangle | Ellipsis;

// Select a particular shape when given its kind
type ellipsis = DiscriminateUnion<Shape, "kind", "ellipsis">;

Equator

Equator: function

An equator that takes to items and checks whether they are equal to each other.

const sameLength : Equator<string> = (lhs, rhs) => lhs.length === rhs.length;
["a", "aa", "aaa"].find(sameLength.bind(null, "me"))
param

The first (left-hand side) item to compare.

param

The second (right-hand side) item to compare.

returns

True iff both items are deemed equal.

Type declaration

    • (lhs: T, rhs: T): boolean
    • Parameters

      • lhs: T
      • rhs: T

      Returns boolean

JSONCompoundValue

JSONCompoundValue: JSONObject | JSONArray

A JSON compound value (JSONArray or JSONObject).

JSONPrimitiveValue

JSONPrimitiveValue: null | undefined | string | number | boolean | Date

A primitive JSON value.

JSONValue

A JSON value (primitive or compound).

KeyExtractor

KeyExtractor: function

Extracts a key from an object used for comparing the object to other objects.

class Customer {
  constructor(public id: number, public name: string) {}

  static keyId(customer: Customer): number {
    return customer.id;
  }

  static keyName(customer: Customer): string {
    return customer.name;
  }
}

const collection = new IndexedCollection<Customer>();
const byId = collection.createOrderedIndex<number>({key: Customer.byId});
const byName = collection.createOrderedIndex<string>({key: Customer.byName});
// add some customers
// ...
byId.getAt(9);
byName.getAt("Cleopatra");
param

Object to extract a key from.

returns

The key for the object.

Type declaration

    • (item: T): K
    • Parameters

      • item: T

      Returns K

KeyValuePair

KeyValuePair: Pair<K, V>

A key-value pair as an array tuple. Used eg. by Map#entries.

const Map<number, User> users = new Map();
const entries: Iterable<KeyValuePair<number, User>> = users.entries();

MatchingKeys

MatchingKeys: MatchingKeys<TRecord, TMatch, K>

Gives all property keys whose types match the given type.

interface User {
  active: boolean;
  age: number;
  mail: string;
  name: string;
  username: string;
}

function foo(stringKey: MatchingKeys<User, string>) {
  // Variable stringKey now has the type
  // "mail" | "name" | "username"
  const b1 = stringKey === "mail"; // works
  const b2 = stringKey === "name"; // works
  const b3 = stringKey === "username"; // works
  // [ts] Operator '===' cannot be applied to types '"mail" | "name" | "username"' and '"active"'.
  const b4 = stringKey === "active";
}

// Variable advanced now has the type
// "mail" | "name"
declare const advanced = MatchingKeys<User, string, "age" | "mail" | "name">;

Maybe

Maybe: T | undefined

Represents an optional value. Absence of the value is indicated by undefined. JavaScript provides tow different types for the notion of absende, namely null and undefined, with minor semantic differences. However, for the sake of clarity and simplicity, I am in favor of using only one type whenever a value is absent. undefined is suited better as it is the default value when declaring a variable and used for optional function parameters.

Consider enabling strictNullChecks with typescript to check for possibly undefined values.

function getLength(word: Maybe<string>) {
  return word !== undefined ? word.length : 0;
}

Nonuple

Nonuple: [T1, T2, T3, T4, T5, T6, T7, T8, T9]

A 9-tuple with nine elements.

Octuple

Octuple: [T1, T2, T3, T4, T5, T6, T7, T8]

An 8-tuple with eight elements.

Omit

Omit: Pick<T, Exclude<keyof T, K>>

From T omit a set of properties K.

// Takes a vector3 that does not need to have a z-coordinate.
function projectToXY(vector: Omit<Vector3, "z"): Vector2 {
  return {x: vector.x, y: vector.y};
}

Overwrite

Overwrite: object & T2

Takes a type and create a new type with some properties overwritten with a different type.

// Somewhere options are defined, and only an ID is required.
interface Options {
  id: number,
  foo?: string,
  bar?: string,
}

// ...

// Now we want to create a function that takes an `Options` object,
// but with the foo property mandatory.
function createOptions(opts: Overwrite<Options, {foo: string}) {
  console.log(opts.foo) // Now opts.foo cannot be undefined.
}

Pair

Pair: [T1, T2]

A 2-tuple with two elements.

PartialExcept

PartialExcept: Partial<Omit<T, K>> & Pick<T, K>

Makes every property optional, except for the given ones.

interface Entity {
  id: number;
  uuid: string;
}

interface User extends Entity {
  username: string;
  active: boolean;
  age: number;
  mail: string;
  name: string;
  // ...
}

// Same as PartialExcept<User, "id" | "uuid">
function createEntity<T extends Entity>(data: PartialExcept<User, keyof Entity>) {
  // ...
}


createEntity({id: 1, uuid: "foo"}); // works
createEntity({id: 1, age: 9}); // error: property uuid is missing

PartialFor

PartialFor: Omit<T, K> & Partial<Pick<T, K>>

Makes every given property optional.

interface User {
  username: string;
  active: boolean;
  age: number;
  mail: string;
  name: string;
  // ...
}

// Makes the properties age and mail optional.
declare const user: PartialFor<User, "age" | "mail">;

PartialKeys

PartialKeys: AssignableKeys<TRecord, undefined, K>

Shortcut for AssignableKeys<TRecord, undefined, K>. Gives all property keys that are optional, ie. to which undefined can be assigned.

interface Data {
  foo: number;
  bar?: number;
  baz: string|undefined;
}

// "bar"|"baz"
type PartialData = PartialKeys<Data>;

PickAssignable

PickAssignable: Pick<TRecord, AssignableKeys<TRecord, TMatch, K>>

From TRecord, pick a set of properties to which the given type can be assigned.

interface Data {
  foo: string | number;
  bar?: number;
  baz: string;
}

// {foo: string|number, baz: string}
type StringData = PickAssignable<Data, string>;

PickMatching

PickMatching: Pick<TRecord, MatchingKeys<TRecord, TMatch, K>>

From TRecord, pick a set of properties that match the given type.

interface Data {
  foo: string | number;
  bar?: number;
  baz: string;
}

// {baz: string}
type StringData = PickAssignable<Data, string>;

PickPartial

PickPartial: Pick<TRecord, PartialKeys<TRecord, K>>

Pick the set of properties that are optional, eg. to which undefined can be assigned.

abstract class Model<TAttributes> {
 private attributes: TAttributes;
 constructor(attributes: TAttributes) {
   this.attributes = Object.assign({}, this.getDefaults(), attributes);
 }
 // Must return defaults for all optional attributes.
 abstract getDefaults(): Required<PickPartial<TAttributes>>;
}

interface UserAttributes {
  username: string;
  age?: number;
  email?: string;
}

class UserModel extends Model<UserAttributes> {
  getDefaults() {
    return {
      email: "johndoe@example.com",
      age: 18,
    };
  }
}

PickRequired

PickRequired: Pick<TRecord, RequiredKeys<TRecord, K>>

Predicate

Predicate: function

A predicate that takes an items and check for a condition.

const isOdd : Predicate<number> = x => x % 2 === 1;
[1,2,3,4,5,6,7,8,9].filter(isOdd) // => [1,3,5,7,9]
param

Item to test.

returns

The result of the test.

Type declaration

    • (item: T): boolean
    • Parameters

      • item: T

      Returns boolean

Quadruple

Quadruple: [T1, T2, T3, T4]

A 4-tuple with four elements.

Quintuple

Quintuple: [T1, T2, T3, T4, T5]

A 5-tuple with five elements.

ReadonlyExcept

ReadonlyExcept: Readonly<Omit<T, K>> & Pick<T, K>

Makes every given property readonly, except for the given properties.

interface User {
  username: string;
  active: boolean;
  age: number;
  mail: string;
  name: string;
  // ...
}

// Makes all properties but age and mail readonly.
declare const user: ReadonlyExcept<User, "age" | "mail">;

ReadonlyFor

ReadonlyFor: Omit<T, K> & Readonly<Pick<T, K>>

Makes every given property readonly.

interface User {
  username: string;
  active: boolean;
  age: number;
  mail: string;
  name: string;
  // ...
}

// Makes the properties age and mail readonly.
declare const user: ReadonlyFor<User, "age" | "mail">;

RemoveFrom

RemoveFrom: Pick<T, Exclude<keyof T, keyof K>>

A type without all properties of the other type.

interface Options {
  id: number;
  name: string;
  mail: string;
}

interface InternalOptions {
  id: number;
}

let idProvider = 0;
function createOptions<T>(additionalOptions: Partial<RemoveFrom<Options, InternalOptions>> = {}): Options {
  return {
    id: idProvider++,
    mail: additionalOptions.mail || "foo@example.com"
    name: additionalOptions.name || "foo",
  };
}

// ...

const opts1 = createOptions({name: "blutorange"}); // WORKS
const opts2 = createOptions({id: 1}) // TYPE ERROR

RequiredFor

RequiredFor: Omit<T, K> & Required<Pick<T, K>>

RequiredKeys

RequiredKeys: UnassignableKeys<TRecord, undefined, K>

Runnable

Runnable: function

A runnable is a function performs some operation when it is called, possibly with side effects, but does not return any value.

function runTest(test: Runnable) {
  const t1 = Date.now();
  try {
    test();
    console.log("Test successful");
  }
  catch(e) {
    console.log("Test failed.");
  }
  finally {
    const t2 = Date.now();
    console.log(`Took ${(b-a)/1000} s`);
  }
}

runTest( () => JSON.parse(inputData) );

Type declaration

    • (): void
    • Returns void

Septuple

Septuple: [T1, T2, T3, T4, T5, T6, T7]

A 7-tuple with seven elements.

Sextuple

Sextuple: [T1, T2, T3, T4, T5, T6]

A 6-tuple with six elements.

Single

Single: [T1]

A 1-tuple with one element.

StringObject

StringObject: object

Similar to typescripts built-in type Record, but with the order of type parameters reverse and the keys being optional.

An object with string keys and a given value type. Optionally, you can limit the available keys to a set of given keys.

const obj: StringObject<boolean> = {
  foo: true,
  bar: false,
  foobar: false,
};

const obj2: StringObject<boolean, "foo" | "bar"> = {
  foo: true,
  bar: false,
  // Object literal may only specify known properties, and 'foobar'
  // does not exist in type 'StringObject<boolean, "foo" | "bar">'.
  foobar: false
};

Type declaration

Supplier

Supplier: function

A supplier produces a value without an explicit input.

// A logging function for messages that may be costly to produce, eg. that
// may involve serialzing a deep object graph for debugging purposes. A
// supplier can be used to create the logging message only when the logging
// level is set to debug.
function debug(messageSupplier: Supplier<string>): void {
  if (loggingLevel === "debug") {
    console.debug(messageSupplier());
  }
}

Type declaration

    • (): T
    • Returns T

TriConsumer

TriConsumer: function

Same as Consumer, but accepts three items to be consumed.

see

Consumer

Type declaration

    • (item1: T1, item2: T2, item3: T3): void
    • Parameters

      • item1: T1
      • item2: T2
      • item3: T3

      Returns void

TriPredicate

TriPredicate: function

Same as Predicate, but accepts three parameters.

Type declaration

    • (item1: T1, item2: T2, item3: T3): boolean
    • Parameters

      • item1: T1
      • item2: T2
      • item3: T3

      Returns boolean

TriSupplier

TriSupplier: function

Same as a Supplier, but returns three items.

see

Supplier

Type declaration

Triple

Triple: [T1, T2, T3]

A 3-tuple with three elements.

TypedBiFunction

TypedBiFunction: function

Same as TypedFunction, but takes two arguments.

see

TypedFunction

Type declaration

    • (arg1: TParam1, arg2: TParam2): TReturn
    • Parameters

      • arg1: TParam1
      • arg2: TParam2

      Returns TReturn

TypedFunction

TypedFunction: function

A function that takes a single argument and returns a value.

const stringLength;
["foo", "bar", "foobar"].map(stringLength);
typeparam

Type of the function's argument.

typeparam

Type of the function's return value.

Type declaration

    • (arg: TParam): TReturn
    • Parameters

      • arg: TParam

      Returns TReturn

TypedTriFunction

TypedTriFunction: function

Same as TypedFunction, but takes three arguments.

see

TypedFunction

Type declaration

    • (arg1: TParam1, arg2: TParam2, arg3: TParam3): TReturn
    • Parameters

      • arg1: TParam1
      • arg2: TParam2
      • arg3: TParam3

      Returns TReturn

UnaryOperator

UnaryOperator: TypedFunction<T, T>

An operator takes an item of a given type and computes a result of the same type.

const negate: UnaryOperator<number> = x => -x;
[1,2,3,4,5].map(negate);
typeparam

of the domain on which the operator operates.

UnassignableKeys

UnassignableKeys: UnassignableKeys<TRecord, TMatch, K>

UnionMap

UnionMap: object

Given a discriminated (tagged) union, creates a map between the tag (discriminant) and the corresponding type.

interface Square {
  kind: "square",
  geometry: {
    side: number;
  }
}

interface Circle {
  kind: "circle",
  geometry: {
    radius: number;
  }
}

interface Rectangle {
  kind: "rectangle",
  geometry: {
    horizontalSide: number;
    verticalSide: number;
  }
}

interface Ellipsis {
  kind: "ellipsis",
  geometry: {
    horizontalHalfAxis: number;
    verticalHalfAxis: number;
   }
}

// Union of all shapes
type Shape = Square | Circle | Rectangle | Ellipsis;

// Resolves to {square: Square, circle: Circle, rectangle: Rectangle, ellipsis: Ellipsis}
type kindToShape = UnionMap<Shape, "kind">;

Type declaration

Voidable

Voidable: T | void

An optional return type for functions that must either explicitly return a value of a certain type; or not have a return statement. Note that a function without a return statement will return undefined when called.

Contrast this with Maybe<T>: Even if the return type is declared as undefined, the function must still contain an explicit return undefined statement.

interface UndoableAction {
  perform(): Voidable<Promise<void>>;
  undo(): Voidable<Promise<void>>;
}

The above interface defines an action that can be undone. An action never return a value, it only performs some side effects. It also supports asynchronous actions by returning a promise.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc