io-ts-path
v1.1.1
Published
type-safe paths for io-ts models
Downloads
13
Maintainers
Readme
io-ts-path
Generate type-safe paths from io-ts models.
Basic usage
Type-safe path from io-ts type
import * as t from 'io-ts';
import { query, path } from 'io-ts-path';
const Person = t.type({
name: t.string,
age: t.number,
fullName: t.type({
first: t.string,
last: t.string
})
});
path(query(Person).name)
// => ['name']
path(query(Person).fullName.first)
// => ['fullName', 'first']
Usage with dictionaries and arrays:
import * as t from 'io-ts';
import { query, path, WildCard } from 'io-ts-path';
const Model = t.type({
users: t.dictionary(
t.string,
t.type({
name: t.string,
age: t.number,
books: t.array(t.string),
}),
),
});
path(query(Model).users._.books)
// => ['users', WildCard, 'books']
io-ts type from path
import * as t from 'io-ts';
import { type } from 'io-ts-path';
const Person = t.type({
name: t.string,
age: t.number,
fullName: t.type({
first: t.string,
last: t.string
})
});
type(Person, ['age'])
// => t.number
type(Person, ['fullName', 'first'])
// => t.string
type(Person, ['foo'])
// => throws PathError
import * as t from 'io-ts';
import { type, WildCard } from 'io-ts-path';
const Model = t.type({
users: t.dictionary(
t.string,
t.type({
name: t.string,
age: t.number,
books: t.array(t.string),
}),
),
});
type(Model, ['users', WildCard, 'books'])
// => t.array(t.string)