io-ts-path
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

io-ts-path

CircleCI npm version codecov

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)

Package Sidebar

Install

npm i io-ts-path

Weekly Downloads

1

Version

1.1.1

License

Zlib

Unpacked Size

21.6 kB

Total Files

16

Last publish

Collaborators

  • marcello