TypeScript type builder
An extensible TypeScript type builder that produces a function to verify the type. Using an editor compatible with TypeScript will enable autocomplete with the correct types.
Note: if the TypeScript strictNullChecks
option is set to false, then every type will always be optional, even though the checker function will not allow null
or undefined
. It is best to have the option set to true.
Installation:
npm i type-builder
Example:
; ;type; ;type; ;type; ; if !isUserdata else
Expected output:
Alice's profile is version 2. Their websites: example.com
Hello world!: Hello world!
And TypeScript will correctly infer the type of data
.
TypeChecker
This type represents a function that checks whether a passed argument is of type T
. Each of the types in the next section are or return a TypeChecker
. Additionally the type has a property TypeChecker<T>.type
whose type is T
, which can be used to get the type (i.e. by using typeof checker.type
). This property does not actually exist on the type checkers.
Built-in types:
bool
Checks whether a passed argument is a boolean.
num
Checks whether a passed argument is a number.
str
Checks whether a passed argument is a string.
literal(x)
Checks whether a passed argument is equivalent to the provided literal x
. x
must be a boolean, number, or string.
array(type)
Checks whether a passed argument is an array that only contains the provided type
. type
can be another TypeChecker
, or it can be a boolean, number, or string literal.
Examples:
array(5)
: checks whether an array contains only the number5
. Equivalent toarray(literal(5))
.array(str)
: checks whether an array contains only strings.array(array(num))
: checks whether an array contains only arrays of numbers.
dict(type)
Checks whether a passed argument is a dictionary with the properties defined in type
. Each property of type
may be another TypeChecker
, or it can be a boolean, number, or string literal.
Examples:
dict({a: optional(5), b: str, c: "some text"})
strDict(type)
Checks whether every property inside an object is of type type
. type
may be another TypeChecker
, or it can be a boolean, number, or string literal.
Examples:
strDict(array(num))
: checks whether every parameter in an object is an array of numbers.
optional(type)
Checks whether a passed argument is either undefined
/null
or is of type type
. type
may be another TypeChecker
, or it can be a boolean, number, or string literal.
union(types)
Checks whether a passed argument matches any of the types in types
. types
must be an array containing TypeChecker
s, boolean literals, number literals, or string literals.
Examples:
union([str, num])
: checks whether the passed argument is either a string or a number.
intersection(types)
Checks whether a passed argument matches all of the types in types
. types
must be an array containing TypeChecker
s, boolean literals, number literals, or string literals.
Examples:
intersection([array(str), dict({length: 5})])
: checks whether the passed argument is an array of strings and has alength
of 5.
Making a custom TypeChecker:
A custom TypeChecker
is just a function that returns the type x is T
.
For example, code for a TypeChecker
that verifies whether a string has a certain length could look like this:
;
The cast to TypeChecker<string>
is necessary to make sure a phantom property type
appears on the type of the returned value.