Options
All
  • Public
  • Public/Protected
  • All
Menu

ts-decode

Index

Decode

Infer

Infer<T>: T extends Decoder<infer U> ? U : never

Utility type useful for extracting the return type of a Decoder

const decoder = object({
x: number.required,
y: string.optional,
})


type MyType = Infer<typeof decoder>

/*
Inferred as:

{
x: number,
y?: string | undefined
}

Type parameters

  • T

Result

Result<T>: { error: false; value: T } | { error: true; reason: Reason }

A type representing the result of a decoding process

Type parameters

  • T

Primitives

boolean

boolean: Decoder<boolean> = ...

Decodes a boolean

null_

null_: Decoder<null> = ...

Decodes the value null

null_.decode(null) // => ✅ null
null_.decode(undefined) // => 🟥 "Expected null, got undefined instead"

number

number: Decoder<number> = ...

Decodes a number

number.decode(42) // => ✅ 42
number.decode("42") // => 🟥 "Expected a number, got \"42\" instead"

string

string: Decoder<string> = ...

Decodes a string

undefined_

undefined_: Decoder<undefined> = ...

Decodes the value undefined

undefined_.decode(undefined) // => ✅ undefined
undefined_decode(null) // => 🟥 "Expected undefined, got null instead"

unknown

unknown: Decoder<unknown> = ...

Leave the value as it is without making any assumptions about its type. Useful for dealing with types later

unknown.decode([1, 2, 3]) // => ✅ [1, 2, 3]

hardcoded

  • hardcoded<T>(constant: T): Decoder<T>
  • Decodes an exact value. Useful in combination with oneOf for creating enums.

    const dec = hardcoded("TAG") // => Decoder<"TAG">
    dec.decode("TAG") // => ✅ "TAG"
    dec.decode("not tag") // => 🟥 "Expected "TAG", got \"not tag\" instead"

    Type parameters

    • T: undefined | null | string | number | boolean

    Parameters

    • constant: T

    Returns Decoder<T>

never

  • never(reason: string): Decoder<never>
  • A decoder that never succeeds.

    never("invalid value").decode(42) // => 🟥 "invalid value"
    

    Parameters

    • reason: string

    Returns Decoder<never>

succeed

  • A decoder that always returns the same value, ignoring the given input. This decoder always suceeds.

    succeed(42).decode("ignored value") // =>  ✅ 42
    

    Type parameters

    • T

    Parameters

    • value: T

    Returns Decoder<T>

Higher order decoders

Field

Field<T>: RequiredField<T> | OptionalField<T>

Type parameters

  • T

ObjectSpecs

ObjectSpecs: {}

A type representing the fields (either optional or mandatory) required by the object decoder

Type declaration

  • [key: string]: Field<unknown>

array

  • Decodes an array using the given decoder

    array(number).decode([1, 2, 3]) // => ✅ [1, 2, 3]
    array(number).decode([1, null, 3]) // => 🟥

    Type parameters

    • T

    Parameters

    Returns Decoder<T[]>

dict

  • Given a Decoder of type T, decodes a object as a string => T map. Useful when keys are not statically known

    dict(number).decode({ x: 0, y: 1 }) // => ✅ { x: 0, y: 1 }
    dict(number).decode({ x: 0, y: "str" }) // => 🟥

    dict(number) // Decoder<Partial<{ [key: string]: number | undefined; }>>

    Type parameters

    • T

    Parameters

    Returns Decoder<{}>

lazy

  • Wraps a decode into a thunk. Useful for creating decoders for recursive data structures

    type Tree = {
    label: string;
    children: Tree[];
    };

    const treeDecoder: Decoder<Tree> = object({
    label: string.required,
    children: lazy(() => array(treeDecoder)).required,
    });

    Type parameters

    • T

    Parameters

    Returns Decoder<T>

object

oneOf

  • oneOf<T>(...decoders: T): Decoder<OneOf<T>>
  • Tries each of the given decoders

    const dec = oneOf(number, string) // => Decoder<number | string>

    dec.decode("hi") // => ✅ "hi"
    dec.decode(1234) // => ✅ 1234
    dec.decode(true) // => 🟥

    Type parameters

    Parameters

    • Rest ...decoders: T

    Returns Decoder<OneOf<T>>

tuple

  • tuple<T>(...decoders: T): Decoder<Tuple<T>>
  • Decodes an array with a fixed number of arguments

    tuple(number, string).decode([1, "x"]) // => ✅ [1, "x"]
    tuple(number, string).decode(["not a number", "x"]) // => 🟥

    Type parameters

    Parameters

    • Rest ...decoders: T

    Returns Decoder<Tuple<T>>

Handle failure

Reason

Reason: { reason: string; type: "FAIL" } | { reasons: Reason[]; type: "ONE_OF" } | { index: number; reason: Reason; type: "ARRAY" } | { field: string; type: "MISSING_FIELD" } | { field: string; reason: Reason; type: "FIELD_TYPE" }

A type representing the reason of a decoding failure

reasonToJsonString

  • reasonToJsonString(reason: Reason): string
  • Convert to a json-like string

    Parameters

    Returns string

reasonToXmlString

  • reasonToXmlString(reason: Reason): string
  • Convert to a xml-like string

    Parameters

    Returns string

Generated using TypeDoc