Options
All
  • Public
  • Public/Protected
  • All
Menu

Teach the compiler about type checking for constrained types.

Index

Type aliases

Age

Age: Tagged<"Age">

Age (usually of people)

Byte

Byte: Tagged<"Byte"> | Tagged<"Integer">

An integer between 0 and 255, inclusive.

Degrees

Degrees: Tagged<"Degrees">

A number between 0 and 360, inclusive of 0, exclusive of 360.

Other values are mathematically valid, but are constrained here for implementation convenience and reliability. Use mod360 to coerce to this range.

IAge

IAge: Tagged<"IAge">

Integer age (usually of people)

Integer

Integer: Tagged<"Integer">

An integer.

Money

Money: Tagged<"Money">

An amount of money.

Probability

Probability: Tagged<"Probability">

The unit interval: a number between 0 and 1, inclusive.

Rate

Rate: Tagged<"Rate">

Interest/growth rate as a fraction (not percent).

Relaxed

Relaxed<T, Tag>: T extends Tagged<Tag, infer Base> ? Base : T extends Tagged<Tag, infer Base> ? Base | undefined : T extends {} ? {[ k in keyof T]: Relaxed<T[k], Tag> } : T

Relax the type checking on tagged types. You can use typeguards and validators to convert back to the unrelaxed version by checking.

Type parameters

  • T

    the type being relaxed.

  • Tag: string = TagOf<T>

    the tag being removed. Defaults for simple types, must be supplied for recursive.

Tag

Tag<T>: { [typetag]: T }

Create a tag for a tagged type. A tagged type is a compile-time-only thing that limits the intensional use of a type, and traces type validations.

Type parameters

  • T: string

Type declaration

  • [typetag]: T

TagOf

TagOf<T>: T extends Tag<infer Tag> ? Tag : never

The tag (as a string literal type) of a tagged type.

Type parameters

  • T

Tagged

Tagged<TagType, Base>: Base & Tag<TagType>

Create a taggged type. A tagged type is is a compile-time-only thing that limits the intensional use of a type, and traces type validations.

Type parameters

  • TagType: string

  • Base = number

TaxRate

TaxRate: Tagged<"TaxRate">

A tax rate (as a fraction, not percentage).

TypeAssertion

TypeAssertion<T, B>: (n: B) => Tagged<T, B>

Type parameters

  • T: string

  • B

Type declaration

    • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

      Parameters

      • n: B

      Returns Tagged<T, B>

TypeCoercion

TypeCoercion<T>: (n: any) => T

Type parameters

  • T

Type declaration

    • (n: any): T
    • A function that coerces to a specified type.

      Parameters

      • n: any

      Returns T

TypeGuard

TypeGuard<T>: (n: any) => n is T

Type parameters

  • T

Type declaration

    • (n: any): n is T
    • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

      Parameters

      • n: any

      Returns n is T

Unit

Unit: Tagged<"Unit">

The unit interval: a number between 0 and 1, inclusive.

Untag

Untag<T>: Omit<T, typeof typetag>

The base type of T without the Tag.

Type parameters

  • T: Tag<string>

Year

Year: Tagged<"Year">

Year as an integer.

Functions

Const as

  • as<T, B>(n: B): Tagged<T, B>
  • A type-inferred cast to a Tagged type. This is primarily intended for use with constants, where supplying the correct type can be determined by inspection

    Generally the type parameters will be inferred.

    let foo: Integer = as(1);
    

    Type parameters

    • T: string

      the tag type

    • B

      the base (untagged) type.

    Parameters

    • n: B

      a value in the base type

    Returns Tagged<T, B>

    n cast to be of the expected Tagged<T,B> type.

asAge

  • asAge(n: number): Tagged<"Age", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Age", number>

asByte

  • asByte(n: number): Tagged<"Byte", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Byte", number>

asDegrees

  • asDegrees(n: number): Tagged<"Degrees", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Degrees", number>

asIAge

  • asIAge(n: number): Tagged<"IAge", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"IAge", number>

asInteger

  • asInteger(n: number): Tagged<"Integer", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Integer", number>

asMoney

  • asMoney(n: number): Tagged<"Money", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Money", number>

Const asNumber

  • asNumber(n: any): number
  • Coerce an unknown value to a number. Strings and other things coercible to numbers will be converted. An error will be thrown if the result does not satisfy isNumber.

    Parameters

    • n: any

      a number, or something coerced to a number

    Returns number

asProbability

  • asProbability(n: number): Tagged<"Probability", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Probability", number>

asRate

  • asRate(n: number): Tagged<"Rate", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Rate", number>

Const asString

  • asString(n: any): string
  • Coerce an unknown value to a string. Numbers will be converted.

    Parameters

    • n: any

      a number, or something coerced to a number

    Returns string

asTaxRate

  • asTaxRate(n: number): Tagged<"TaxRate", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"TaxRate", number>

asUnit

  • asUnit(n: number): Tagged<"Unit", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Unit", number>

asYear

  • asYear(n: number): Tagged<"Year", number>
  • Assert that the argument n is of the type Tagged<T,B>. The compiler's type inferencing is informed that the return value has been validated to be of that type.

    Parameters

    • n: number

    Returns Tagged<"Year", number>

Const ceil

Const decr

Const floor

Const idiv

Const imod

Const incr

isAge

  • isAge(n: any): n is Tagged<"Age", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Age", number>

isByte

  • isByte(n: any): n is Tagged<"Byte", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Byte", number>

isDegrees

  • isDegrees(n: any): n is Tagged<"Degrees", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Degrees", number>

isIAge

  • isIAge(n: any): n is Tagged<"IAge", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"IAge", number>

isInteger

  • isInteger(n: any): n is Tagged<"Integer", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Integer", number>

isMoney

  • isMoney(n: any): n is Tagged<"Money", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Money", number>

Const isNumber

  • isNumber(n: any): n is number
  • Type guard for numbers.

    Parameters

    • n: any

    Returns n is number

    true if the argument n is a number, not a NaN, and finite.

isProbability

  • isProbability(n: any): n is Tagged<"Probability", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Probability", number>

isRate

  • isRate(n: any): n is Tagged<"Rate", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Rate", number>

Const isString

  • isString(n: any): n is string
  • Type guard for strings.

    Parameters

    • n: any

    Returns n is string

    true if the argument n is a string.

isTaxRate

  • isTaxRate(n: any): n is Tagged<"TaxRate", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"TaxRate", number>

isUnit

  • isUnit(n: any): n is Tagged<"Unit", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Unit", number>

isYear

  • isYear(n: any): n is Tagged<"Year", number>
  • A type guard. Returns true if the argument n satisfies the conditions to be of type T, and informs the compiler's type inferencing engine.

    Parameters

    • n: any

    Returns n is Tagged<"Year", number>

Const isum

Const mod360

  • Coerce a number to the range between 0 and 360, inclusive of 0, exclusive of 360.

    Parameters

    • n: number

    Returns Degrees

    the number, coerced to the range (0,360], typed as Degrees

Const numberRange

  • Create functions to test and assert membership in a tagged numerical range.

    Type parameters

    • T: string

    Parameters

    • tag: T

      the type tag

    • range: NumberDomain

      the specification of the range to be enforced.

    Returns DomainFns<T, number>

    a DomainFns with the test and assert functions.

Const round

toAge

  • toAge(n: any): Tagged<"Age", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Age", number>

toByte

  • toByte(n: any): Tagged<"Byte", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Byte", number>

toDegrees

  • toDegrees(n: any): Tagged<"Degrees", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Degrees", number>

toIAge

  • toIAge(n: any): Tagged<"IAge", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"IAge", number>

toInteger

  • toInteger(n: any): Tagged<"Integer", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Integer", number>

toMoney

  • toMoney(n: any): Tagged<"Money", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Money", number>

toProbability

  • toProbability(n: any): Tagged<"Probability", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Probability", number>

toRate

  • toRate(n: any): Tagged<"Rate", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Rate", number>

toTaxRate

  • toTaxRate(n: any): Tagged<"TaxRate", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"TaxRate", number>

toUnit

  • toUnit(n: any): Tagged<"Unit", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Unit", number>

toYear

  • toYear(n: any): Tagged<"Year", number>
  • A function that coerces to a specified type.

    Parameters

    • n: any

    Returns Tagged<"Year", number>

Const trunc

Const widenAs

Const widenIs

  • widenIs<T>(guard: TypeGuard<T>): (n: any) => n is undefined | T
  • Convert a TypeGuard for type T into one for T | undefined.

    Type parameters

    • T

    Parameters

    Returns (n: any) => n is undefined | T

      • (n: any): n is undefined | T
      • Parameters

        • n: any

        Returns n is undefined | T

Const widenTo

Generated using TypeDoc