Home Reference Source Test
import {TinyType} from 'tiny-types/lib/TinyType'
public class | source

TinyType

The TinyType abstract class should be used as a base class for your own Tiny Types.

If you want the Tiny Type to wrap a single value use the TinyTypeOf instead as it will save you some keystrokes.

Example:

class FirstName extends TinyTypeOf<string>() {}
class LastName  extends TinyTypeOf<string>() {}
class Age       extends TinyTypeOf<number>() {}

class Person extends TinyType {
  constructor(public readonly firstName: FirstName,
              public readonly lastName:  LastName,
              public readonly age:       Age,
  ) {
    super();
  }
}

Test:

Method Summary

Public Methods
public

equals(another: TinyType): boolean

Compares two tiny types by value

public

toJSON(): JSONValue

Serialises the object to a JSON representation.

public

Serialises the object to its string representation

Public Methods

public equals(another: TinyType): boolean source

Compares two tiny types by value

Params:

NameTypeAttributeDescription
another TinyType

Return:

boolean

Example:

Comparing simple types
class Id extends TinyTypeOf<string>() {}

const id = new Id(`3cc0852d-fda7-4f61-874e-0cfadbd6182a`);

id.equals(id) === true
Comparing complex types recursively
class FirstName extends TinyTypeOf<string>() {}
class LastName  extends TinyTypeOf<string>() {}
class Age       extends TinyTypeOf<number>() {}

class Person extends TinyType {
  constructor(public readonly firstName: FirstName,
              public readonly lastName:  LastName,
              public readonly age:       Age,
  ) {
    super();
  }
}

const p1 = new Person(new FirstName('John'), new LastName('Smith'), new Age(42)),
      p2 = new Person(new FirstName('John'), new LastName('Smith'), new Age(42));

p1.equals(p2) === true

Test:

public toJSON(): JSONValue source

Serialises the object to a JSON representation.

Return:

JSONValue

Example:

class FirstName extends TinyTypeOf<string>() {}

const name = new FirstName('Jan');

name.toJSON() === 'Jan'
class FirstName extends TinyTypeOf<string>() {}
class LastName  extends TinyTypeOf<string>() {}
class Age       extends TinyTypeOf<number>() {}

class Person extends TinyType {
  constructor(public readonly firstName: FirstName,
              public readonly lastName:  LastName,
              public readonly age:       Age,
  ) {
    super();
  }
}

const person = new Person(new FirstName('John'), new LastName('Smith'), new Age(42)),

person.toJSON() === { firstName: 'John', lastName: 'Smith', age: 42 }

Test:

public toString(): string source

Serialises the object to its string representation

Return:

string

Test: