Options
All
  • Public
  • Public/Protected
  • All
Menu

TypeShield Documentation

Index

Type aliases

ComparisonResult

ComparisonResult: -1 | 0 | 1 | undefined

The result of an object comparison.

  • 0 if the objects are equal
  • -1 if the current instance is less than the other
  • 1 if the current instance is greater than the other
  • undefined if the objects are not comparable to each other

Constructor

Constructor<T>: object

A constructor

Type parameters

  • T

Type declaration

Email

Email: Tagged<string, "__Email__">

A string that is an email address

ExtractIntersection

ExtractIntersection<T>: ExtractIntersection<T>

Extract a type intersection from a union of guards

Type parameters

  • T

ExtractProperties

ExtractProperties<T>: object

Extract an object of types from an object of guards

Type parameters

Type declaration

ExtractUnion

ExtractUnion<T>: ExtractUnion<T>

Extract a type union from a union of guards

Type parameters

  • T

Guard

Guard<T>: function & HasExpectation

A function that performs a validation on an unknow value and returns a boolean indicating the type of the value.

Type parameters

  • T

Integer

Integer: Tagged<number, "__Integer__">

A number that is an integer

InterfaceValidators

InterfaceValidators<T>: object

Collection of interface validators

Type parameters

  • T

Type declaration

Narrowable

Narrowable: string | number | boolean | undefined | null | void | __type

Union of types that TypeScript can narrow to literal types

Negative

Negative: Tagged<number, "__Negative__">

A number that is negative

NegativeInteger

NegativeInteger: Negative & Integer

A number that is a negative integer

Positive

Positive: Tagged<number, "__Positive__">

A number that is positive

PositiveInteger

PositiveInteger: Positive & Integer

A number that is a positive integer

Tag

Tag<T>: object

A type tag.

Type parameters

  • T: string

Type declaration

Tagged

Tagged<TValue, TTag>: TValue & Tag<TTag>

A tagged type.

Type parameters

  • TValue

  • TTag: string

Validator

Validator: function & HasExpectation

A function that performs a validation on an unknow value and returns a boolean.

Functions

Assert

  • Assert(validator: Validator | function, name?: undefined | string, expectation?: undefined | string): PropertyDecorator
  • A decorator factory that returns a decorator that turns a property into a getter/setter with a setter the asserts the value set matches the validator.

    Parameters

    • validator: Validator | function

      The guard/validator to assert

    • Optional name: undefined | string

      The name to use for the value. Defaults to the class+method name

    • Optional expectation: undefined | string

      An expection message to override the guard/validator expectation

    Returns PropertyDecorator

    The decorator.

and

  • Create a new guard/validator from an intersection of guards/validators

    Type parameters

    Parameters

    • validators: T[] | function

      An array of guards/validators

    Returns Guard<ExtractIntersection<T>>

    A new intersection guard/validator

assert

  • assert(condition: unknown, msg?: string | function): assertscondition
  • Asserts that a condition is true or throws an error if false

    throws

    AssertionError if condition is not true

    Parameters

    • condition: unknown

      The condition to assert

    • Optional msg: string | function

      The message to use in the thrown error. Accepts a function that can be called to generate the message to improve performance.

    Returns assertscondition

assertUnreachable

  • assertUnreachable(msg?: string): never
  • Asserts that the given call is never reached. Will always throw if called. Useful for ending branches that TypeScript can't properly determine

    Parameters

    • Default value msg: string = "Statement should not be reachable"

      The message to throw

    Returns never

assertValue

  • assertValue<T>(guard: Guard<T> | function, value: unknown, name?: undefined | string, expectation?: undefined | string): assertsvalue is T
  • assertValue(validator: Validator | function, value: unknown, name?: undefined | string, expectation?: undefined | string): void
  • Asserts that a value satisfies a provided guard and is therefore of the type returned by the guard.

    Type parameters

    • T

    Parameters

    • guard: Guard<T> | function

      The guard to call with the value

    • value: unknown

      The value to check

    • Optional name: undefined | string

      An optional name for the value to use in the error message

    • Optional expectation: undefined | string

      An optional expectation string to use in the error message. Will override the expectation string attached to the guard.

    Returns assertsvalue is T

  • Asserts that a value satisfies a provided validator.

    Parameters

    • validator: Validator | function

      The validator to call with the value

    • value: unknown

      The value to check

    • Optional name: undefined | string

      An optional name for the value to use in the error message

    • Optional expectation: undefined | string

      An optional expectation string to use in the error message. Will override the expectation string attached to the guard.

    Returns void

hasInterface

  • Creates a guard that tests if a value implements a specified interface

    Type parameters

    • T

    Parameters

    • interfaceName: string

      The interface name to report in the error message

    • validators: InterfaceValidators<T> | function

      The property validators (or function that returns them)

    Returns Guard<T>

    The guard

hasProperties

isAny

  • isAny(value: unknown): value is any
  • Guard that tests if the value is an any value (always true)

    Parameters

    • value: unknown

      The value to test

    Returns value is any

    true

isArray

  • isArray(value: unknown): value is unknown[]
  • Guard that tests if the value is an array

    Parameters

    • value: unknown

      The value to test

    Returns value is unknown[]

    The result of the test

isBigInt

  • isBigInt(value: unknown): value is bigint
  • Guard that tests if the value is an big integer

    Parameters

    • value: unknown

      The value to test

    Returns value is bigint

    The result of the test

isBoolean

  • isBoolean(value: unknown): value is boolean
  • Guard that tests if the value is a boolean

    Parameters

    • value: unknown

      The value to test

    Returns value is boolean

    The result of the test

isComparable

  • isComparable(value: unknown): value is Comparable
  • Guard that tests if the value implements the Comparable interface

    Parameters

    • value: unknown

      The value to test

    Returns value is Comparable

    The result of the test

isDate

  • isDate(value: unknown): value is Date
  • Guard that tests if the value is an instance of Date

    Parameters

    • value: unknown

      The value to test

    Returns value is Date

    The result of the test

isDeepEqualTo

  • isDeepEqualTo<T>(other: T, strict?: boolean): Guard<T>
  • Creates a guard that tests if a value is equal to the specified value using the deep-equal package

    Type parameters

    Parameters

    • other: T

      The object to compare values to

    • Default value strict: boolean = true

      True to use strict equality (default) and false to use coercive equality

    Returns Guard<T>

    The guard

isDefined

  • isDefined<T>(value: T): value is NonNullable<T>
  • Guard that tests if the value is not null and not undefined

    Type parameters

    • T

    Parameters

    • value: T

      The value to test

    Returns value is NonNullable<T>

    The result of the test

isEach

  • Creates a guard that tests if a value is an array and that each value in the array satisfies the given guard.

    Type parameters

    • T

    Parameters

    • eachGuard: Guard<T>

      The guard used for each item in the array.

    Returns Guard<T[]>

    The guard

  • Creates a validator that tests if a value is an array and that each value in the array satisfies the given validator.

    Parameters

    • eachValidator: Validator

      The validator used for each item in the array.

    Returns Validator

    The validator

isEmail

  • isEmail(value: unknown): value is Email
  • Guard that tests if the value is an email address

    Parameters

    • value: unknown

      The value to test

    Returns value is Email

    The result of the test

isEmptyArray

  • isEmptyArray(value: unknown): value is unknown[]
  • Guard that tests if the value is an empty array

    Parameters

    • value: unknown

      The value to test

    Returns value is unknown[]

    The result of the test

isEmptyString

  • isEmptyString(value: unknown): value is ""
  • Guard that tests if the value is an empty string

    Parameters

    • value: unknown

      The value to test

    Returns value is ""

    The result of the test

isEqualTo

  • isEqualTo<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is equal to a specified object. Values are compared by identity first and then by using the Equatable or Comparable interfaces, if implemented.

    Type parameters

    Parameters

    • other: T

      The object to compare values to

    Returns Guard<T>

    The guard

isEquatable

  • isEquatable(value: unknown): value is Equatable
  • Guard that tests if the value implements the Equatable interface

    Parameters

    • value: unknown

      The value to test

    Returns value is Equatable

    The result of the test

isFunction

  • isFunction(value: unknown): value is Function
  • Guard that tests if the value is a function

    Parameters

    • value: unknown

      The value to test

    Returns value is Function

    The result of the test

isGreaterThan

  • isGreaterThan<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is greater than a specified value. Will first compare using the Comparable interface, if implemented and will fall back to operator comparison.

    Type parameters

    • T

    Parameters

    • other: T

      The value to compare to

    Returns Guard<T>

    The guard

isGreaterThanOrEqualTo

  • isGreaterThanOrEqualTo<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is greater than or equal to a specified value. Will first compare using the Comparable interface, if implemented and will fall back to operator comparison. Note that objects not implementing Comparable or custom value representations may return unexpected results as JS will revert to comparing string representations.

    Type parameters

    • T

    Parameters

    • other: T

      The value to compare to

    Returns Guard<T>

    The guard

isIdenticalTo

  • isIdenticalTo<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is identical to another value. This uses the JS strict equality comparison operator (===) so primitives are compared by value but objects are compared by reference.

    Type parameters

    Parameters

    • other: T

      The other value to compare to

    Returns Guard<T>

    The guard

isInstanceOf

isInteger

  • isInteger(value: unknown): value is Integer
  • Guard that tests if the value is an integer

    Parameters

    • value: unknown

      The value to test

    Returns value is Integer

    The result of the test

isLessThan

  • isLessThan<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is less than a specified value. Will first compare using the Comparable interface, if implemented and will fall back to operator comparison.

    Type parameters

    • T

    Parameters

    • other: T

      The value to compare to

    Returns Guard<T>

    The guard

isLessThanOrEqualTo

  • isLessThanOrEqualTo<T>(other: T): Guard<T>
  • Creates a guard that tests if a value is less than or equal to a specified value. Will first compare using the Comparable interface, if implemented and will fall back to operator comparison. Note that objects not implementing Comparable or custom value representations may return unexpected results as JS will revert to comparing string representations.

    Type parameters

    • T

    Parameters

    • other: T

      The value to compare to

    Returns Guard<T>

    The guard

isMatch

  • isMatch(regexp: RegExp): Guard<string>
  • Creates a guard that tests if a value is a string that matches the specified RegExp

    Parameters

    • regexp: RegExp

      The regular expression

    Returns Guard<string>

    The guard

isNegative

  • isNegative(value: unknown): value is Negative
  • Guard that tests if the value is a negative number

    Parameters

    • value: unknown

      The value to test

    Returns value is Negative

    The result of the test

isNegativeInteger

  • isNegativeInteger(value: unknown): value is NegativeInteger
  • Guard that tests if the value is a negative integer

    Parameters

    • value: unknown

      The value to test

    Returns value is NegativeInteger

    The result of the test

isNil

  • isNil(value: unknown): value is unknown
  • Guard that tests if the value is null or undefined

    Parameters

    • value: unknown

      The value to test

    Returns value is unknown

    The result of the test

isNonEmptyArray

  • isNonEmptyArray(value: unknown): value is unknown[]
  • Guard that tests if the value is an array that is not empty

    Parameters

    • value: unknown

      The value to test

    Returns value is unknown[]

    The result of the test

isNonEmptyString

  • isNonEmptyString(value: unknown): value is string
  • Guard that tests if the value is a string that is not empty

    Parameters

    • value: unknown

      The value to test

    Returns value is string

    The result of the test

isNull

  • isNull(value: unknown): value is null
  • Guard that tests if the value is null

    Parameters

    • value: unknown

      The value to test

    Returns value is null

    The result of the test

isNumber

  • isNumber(value: unknown): value is number
  • Guard that tests if the value is a number

    Parameters

    • value: unknown

      The value to test

    Returns value is number

    The result of the test

isObject

  • isObject(value: unknown): value is object
  • Guard that tests if the value is any object (and not null)

    Parameters

    • value: unknown

      The value to test

    Returns value is object

    The result of the test

isPositive

  • isPositive(value: unknown): value is Positive
  • Guard that tests if the value is a positive number

    Parameters

    • value: unknown

      The value to test

    Returns value is Positive

    The result of the test

isPositiveInteger

  • isPositiveInteger(value: unknown): value is PositiveInteger
  • Guard that tests if the value is a positive integer

    Parameters

    • value: unknown

      The value to test

    Returns value is PositiveInteger

    The result of the test

isString

  • isString(value: unknown): value is string
  • Guard that tests if the value is a string

    Parameters

    • value: unknown

      The value to test

    Returns value is string

    The result of the test

isStringContaining

  • isStringContaining(substring: string): Guard<string>
  • Creates a guard that tests if a value is a string containing the specified substring

    Parameters

    • substring: string

      The substring to check for

    Returns Guard<string>

    The guard

isStringNotContaining

  • isStringNotContaining(substring: string): Guard<string>
  • Creates a guard that tests if a value is a string that does not contain the specified substring

    Parameters

    • substring: string

      The substring to check for

    Returns Guard<string>

    The guard

isSymbol

  • isSymbol(value: unknown): value is symbol
  • Guard that tests if the value is a symbol

    Parameters

    • value: unknown

      The value to test

    Returns value is symbol

    The result of the test

isUndefined

  • isUndefined(value: unknown): value is undefined
  • Guard that tests if the value is undefined

    Parameters

    • value: unknown

      The value to test

    Returns value is undefined

    The result of the test

isUnknown

  • isUnknown(value: unknown): value is unknown
  • Guard that tests if the value is an any unknown value (always true)

    Parameters

    • value: unknown

      The value to test

    Returns value is unknown

    true

or

  • Create a new guard/validator from a union of guards/validators

    Type parameters

    Parameters

    • validators: T[] | function

      An array of guards/validators (or function that returns an array)

    Returns Guard<ExtractUnion<T>>

    A new union guard/validator

Generated using TypeDoc