0.5.8 • Published 1 month ago

@symbion/runtype v0.5.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Runtime type system for TypeScript

Description

RunType is a runtime type system for TypeScript.

It was inspired by IO-TS, but I made some opinionated changes in the concept. IO-TS is mathematically correct and follows JavaScript ant TypeScript specifications to the letter. With RunType I wanted to create something more practical.

Some of the changes:

  • I am not too familiar with functional programming concepts, so I don't use them in RunType.
  • The struct combinator handles optional fields easier (without the partial + intersection things in IO-TS)
  • number decoder does not accept NaN.
  • Decoder accepts a config argument and supports type coercion and some other modifiers
  • Validators
  • Runtime type description generation (print() method)

Installation

npm install @symbion/runtype

Usage

This is a work in progress. I plan to write some documentation soon, in the meantime you can look at the test files (src/*.spec.ts) for usage information.

Basic usage

First create a type:

import T from '@symbion/runtype'

const tMyType = T.struct({
	s: T.string,
	n: T.optional(T.number)
})

You can extract a TypeScript type from it:

type MyType = T.TypeOf<typeof tMyType>

You can decode an unknown value:

const u: unknown = { s: 'string', n: 42 }

const decoded = T.decode(tMyType, u)
isOk(decoded)
// = true

const value: MyType = decoded.ok
// = { s: 'string', n: 42 }

Type constructors

TypeTypeScriptRunType
undefinedundefinedT.undefinedValue
nullnullT.nullValue
truetrueT.trueValue
falsefalseT.falseValue
stringstringT.string
numbernumberT.number
integerXT.integer
booleanbooleanT.boolean
datedateT.date
anyanyT.any
unknownunknownT.unknown
literal'a' \| 'b' \| 3T.literal('a', 'b', 3)
optionalType \| undefinedT.optional(tType)
nullableType \| null \| undefinedT.nullable(tType)
arrayArray<Type>T.array(tType)
recordrecord<string, Type>T.record(tType)
struct{ s: string, n: number }T.struct({ s: T.string, n: T.number })
key ofkeyof { s: string, n: number }T.keyof(T.struct({ s: T.string, n: T.number }))
tuple[string, number, Type]T.tuple(T.string, T.number, tType)
unionstring \| number \| TypeT.union(T.string, T.number, tType)
intersectboolean \| trueT.union(T.boolean, T.trueValue)
intersect{ s: string } & { n: number }T.intersect(T.struct({ s: T.string }), T.struct({ n: T.number }))
tagged union{ tag: 's', s: string } \| { tag: 'n', n: number }T.taggedUnion('tag')({ tag: T.literal('s'), s: T.string }, { tag: T.literal('n'), n: T.number })

Helpers

Recursive types

Recursive types can be created with T.lazy() and manual TypeScript types (because TypeScript can't infer recursive types):

interface Recursive {
	name: string
	children: MyType[]
}

const tRecursive: T.Type<Recursive> = T.lazy(() => T.struct({
	name: T.string,
	children: T.array(tRecursive)
}))

Type modifiers

Partial

The T.partial() type modifier takes a Struct type and converts all fields to optional:

const tStruct = T.struct({
	s: T.string,
	n: T.optional(T.number)
})
// = { s: string, n?: number }

const tPartialType = T.partial(tStruct)
// = { s?: string, n?: number }

Patch

The T.patch() type modifier takes a Struct type and converts all optional fields to nullable and all requires fields to optional. It is useful for update APIs, where undefined or missing fields mean not to update and null value means to clear that field.

const tStruct = T.struct({
	s: T.string,
	n: T.optional(T.number)
})
// = { s: string, n?: number }

const tPatchType = T.patch(tStruct)
// = { s?: string, n?: number | null }

Decoder options

The decoder() function accepts an optional config argument. It can be used for type coercion:

T.decode(T.number, '42')
// = { _tag: 'Err', err: [ { path: [], error: 'expected number' } ] }

T.decode(T.number, '42', { coerceStringToNumber: true })
// = isOk(decoded)

All available coercion options:

Option nameType
coerceNumberToStringbooleanCoerce numbers to string
coerceNumberToBooleanbooleanCoerce numbers to boolean
coerceStringToNumberbooleanCoerce string to number
coerceScalarboolean= coerceNumberToString, coerceNumberToBoolean, coerceStringToNumber
coerceStringToDatebooleanCoerce string to Date
coerceNumberToDatebooleanCoerce numbers to Date
coerceDateboolean= coerceStringToDate, coerceNumberToDate
coerceAllbooleanAll the above coercion
acceptNaNbooleanMake T.number accept NaN as number
unknownFields'reject' \| 'drop' \| 'discard'How to treat unknown fields. (reject: error, drop: drops unknown fields from the output, discard: leaves in output as is)

Validation

The decode() function does type decoding, which is a synchron function. Runtype also handles data validation, what is defined as an asynchron function. The type constructors define some validator methods and user defined functions can also be used.

const tMyType = struct({
	s: T.string.minLength(2)
})

Validation works like decoding:

T.validate(T.string.minLength(2), 'abc')
// = { _tag: 'Ok', ok: 'abc' }

T.decode(T.string.minLength(2), 'a')
// = { _tag: 'Ok', ok: 'a' }
T.validate(T.string.minLength(2), 'a')
// = { _tag: 'Err', err: [ { path: [], error: 'length must be at least 2' } ] }

Awailable validators:

String validators

ValidatorDescription
in(value1, value2, ...)Value is one of value1, value2, ....
minLength(length)Length is at least length
maxLength(length)Length is at most length
matches(pattern)Value matches pattern
email()Value is an email address

Number validators

ValidatorDescription
in(value1, value2, ...)Value is one of value1, value2, ....
integer()Value is an integer
min(minValue)Value is at least minValue
max(maxValue)Value is at most maxValue
between(minValue, maxValue)Value is between minValue and maxValue

Boolean validators

ValidatorDescription
true()Value is true
false()Value is false

Literal validators

ValidatorDescription
in(value1, value2, ...)Value is one of value1, value2, ....

Custom validators

function max42(v: number | undefined) {
	return (v || 0) <= 42 ? T.ok(v) : T.error("Max 42 is allowed!")
}

await T.validate(T.number.addValidator(max42), 43)
// = { _tag: 'Err', err: [ { path: [], error: "Max 42 is allowed!" } ] }

Internals

Missing properties vs undefined

TypeScript (because of JavaScript) differentiates missing properties and properties with undefined value. This is sometimes useful, however it makes it more difficult to handle this in runtime type systems. Take the following simple TypeScript type:

interface Person {
	name: string
	age?: number
}

In IO-TS you can create it like this:

const tPerson = T.intersection([
	T.type({
		name: T.string
	}),
	T.partial({
		age: T.number
	})
type Person = T.TypeOf<typeof tPerson>
])

RunType uses complex TypeScript mechanisms to achieve a simpler and readable syntax:

const tPerson = T.struct({
	name: T.string
	age: T.optional(T.number)
])
type Person = T.TypeOf<typeof tPerson>

Under the hood RunType generates the same intersection type beacause of limitations in TypeScipt, but it works the same as the original type:

type Person = { name: string } & { age?: number }

Closing thoughts

If you want to boost your TypeScript knowledge to the next level I highly recommend to write a runtime type system. I guarantee it will be fun! :)

0.5.8

1 month ago

0.5.7

6 months ago

0.5.4

1 year ago

0.5.6

1 year ago

0.5.5

1 year ago

0.5.3

1 year ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago