Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface LiteralIoType<T, K>

Type parameters

Hierarchy

Index

Properties

LiteralType

LiteralType: LiteralTypeConstructor = class<TT, K extends Type<any> = Type<any>> implements IoType<TT> {readonly name: Name = name;readonly type!: K;readonly value!: TT;private _options: Lazy<LiteralTypeOptions<TT, K>>;constructor(options: Lazy<LiteralTypeOptions<TT, K>>) {this._options = options;if (typeof options !== "function") {this._applyOptions();} else {lazyProperties(this, this._applyOptions, ["type", "value"]);}}read<R>(reader: Reader<R>, raw: R): TT {if (this.type.read === undefined) {throw new Incident("NotReadable", {type: this});}return reader.trustInput ? this.clone(this.value) : this.type.read(reader, raw);}write<W>(writer: Writer<W>, value: TT): W {if (this.type.write === undefined) {throw new Incident("NotWritable", {type: this});}return this.type.write(writer, value);}testError(val: TT): Error | undefined {const error: Error | undefined = testError(this.type, val);if (error !== undefined) {return error;}if (!this.type.equals(val, this.value)) {return Incident("InvalidLiteral", "Invalid literal value");}return undefined;}test(value: TT): boolean {return this.type.test(value) && this.type.equals(value, this.value);}equals(val1: TT, val2: TT): boolean {return this.type.equals(val1, val2);}clone(val: TT): TT {return this.type.clone(val);}diff(_oldVal: TT, _newVal: TT): undefined {return;}patch(oldVal: TT, _diff: undefined): TT {return this.type.clone(oldVal);}reverseDiff(_diff: Diff | undefined): undefined {return;}squash(_diff1: undefined, _diff2: undefined): undefined {return;}private _applyOptions(): void {if (this._options === undefined) {throw createLazyOptionsError(this);}const options: LiteralTypeOptions<TT, K> = typeof this._options === "function"? this._options(): this._options;const type: K = options.type;const value: TT = options.value;Object.assign(this, {type, value});}}

You may need to explicitly write the type or inference won't pick it. For example, in the case of enum values, inference will pick the type of the enum instead of the specific property you pass.

see

https://github.com/Microsoft/TypeScript/issues/10195

Optional name

name: undefined | string

Name of this type. This is only used to help with debugging.

type

type: K

value

value: T

Methods

clone

  • clone(value: T): T
  • Returns a deep copy of value.

    Parameters

    • value: T

      The value to clone, trusted to be compatible with this type.

    Returns T

    A deep copy of the supplied value, restricted to the properties of this type.

equals

  • equals(left: T, right: T): boolean
  • Tests if left is equal to value.

    This is a deep strict structural equality test restricted to the properties of this type.

    It satisfies the following properties (the variables a, b and c are valid values):

    • Reflexivity: type.equal(a, a) === true
    • Symmetry: type.equals(a, b) === type.equals(b, a)
    • Transitivity: if type.equals(a, b) && type.equals(b, c) then type.equals(a, c)

    The above properties mean that type objects implement the Setoid algebra as specified by Static Land.

    see

    https://github.com/rpominov/static-land/blob/master/docs/spec.md#setoid

    Parameters

    • left: T

      Left value, trusted to be compatible with this type.

    • right: T

      Right value, trusted to be compatible with this type.

    Returns boolean

    Boolean indicating if both values are equal.

Optional lte

  • lte(left: T, right: T): boolean
  • Compares two valid values.

    Parameters

    • left: T

      Left operand, a valid value.

    • right: T

      Right operand, a valid value.

    Returns boolean

    Boolean indicating if left <= right is true.

read

  • read<R>(reader: Reader<R>, raw: R): T

test

  • test(value: T): boolean
  • Tests if this type matches value.

    Parameters

    • value: T

      The value to test against this type.

    Returns boolean

    Boolean indicating if this type matches value.

Optional testError

  • testError(value: T): Error | undefined
  • Tests if this type matches value, describes the error if not.

    Parameters

    • value: T

      The value to test against this type.

    Returns Error | undefined

    If this type matches value then undefined; otherwise an error describing why this type does not match value.

write

  • write<W>(writer: Writer<W>, value: T): W

Generated using TypeDoc