Types
Primitive
BigInt
import * as ft from "funtypes";
export const MyBigInt = ft.BigInt;
// => ft.Codec<bigint>
// β
Valid value
assert.deepEqual(ft.BigInt.parse(42n), 42n);
// π¨ Wrong type
assert.deepEqual(
ft.BigInt.safeParse("true"),
{
success: false,
message: `Expected bigint, but was "true" (i.e. a string literal)`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.BigInt.safeParse(42),
{
success: false,
message: `Expected bigint, but was 42`
}
)
Boolean
import * as ft from "funtypes";
export const MyBool = ft.Boolean;
// => ft.Codec<boolean>
// β
Valid value
assert.deepEqual(ft.Boolean.parse(true), true);
assert.deepEqual(ft.Boolean.parse(false), false);
// π¨ Wrong type
assert.deepEqual(
ft.Boolean.safeParse("true"),
{
success: false,
message: `Expected boolean, but was "true" (i.e. a string literal)`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.Boolean.safeParse(42),
{
success: false,
message: `Expected boolean, but was 42`
}
)
Number
import * as ft from "funtypes";
export const MyNumber = ft.Number;
// => ft.Codec<number>
// β
Valid value
assert.deepEqual(ft.Number.parse(42), 42);
assert.deepEqual(ft.Number.parse(3.14), 3.14);
// π¨ Wrong type
assert.deepEqual(
ft.Number.safeParse("42"),
{
success: false,
message: `Expected number, but was "42" (i.e. a string literal)`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.Number.safeParse(true),
{
success: false,
message: `Expected number, but was true`
}
)
Function
import * as ft from "funtypes";
export const MyFunction = ft.Function;
// => ft.Codec<(...args: any[]) => any>
// β
Valid value
const myFun = () => 42;
assert.deepEqual(ft.Function.parse(myFun), myFun);
// π¨ Wrong type
assert.deepEqual(
ft.Function.safeParse("42"),
{
success: false,
message: `Expected function, but was "42" (i.e. a string literal)`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.Function.safeParse(true),
{
success: false,
message: `Expected function, but was true`
}
)
Functions accept and return any
There's unfortunately no way for us to verify what parameters a function accepts or what it returns, so ft.Function just checks it is some kind of function, not anything more specific.
String
import * as ft from "funtypes";
export const MyString = ft.String;
// => ft.Codec<string>
// β
Valid value
assert.deepEqual(ft.String.parse("hello world"), "hello world");
// π¨ Wrong type
assert.deepEqual(
ft.String.safeParse(42),
{
success: false,
message: `Expected string, but was 42`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.String.safeParse(true),
{
success: false,
message: `Expected string, but was true`
}
)
Symbol
import * as ft from "funtypes";
export const MySymbol = ft.Symbol;
// => ft.Codec<symbol>
// β
Valid value
const mySym = Symbol("My Symbol");
assert.deepEqual(ft.Symbol.parse(mySym), mySym);
// π¨ Wrong type
assert.deepEqual(
ft.Symbol.safeParse("true"),
{
success: false,
message: `Expected symbol, but was "true" (i.e. a string literal)`
}
)
// π¨ Wrong type
assert.deepEqual(
ft.Symbol.safeParse(42),
{
success: false,
message: `Expected symbol, but was 42`
}
)