https://github.com/marcello3d/io-ts-path
Generate type-safe paths from io-ts models.
https://github.com/marcello3d/io-ts-path
Last synced: 22 days ago
JSON representation
Generate type-safe paths from io-ts models.
- Host: GitHub
- URL: https://github.com/marcello3d/io-ts-path
- Owner: marcello3d
- Created: 2019-01-18T05:53:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-01T05:04:02.000Z (over 6 years ago)
- Last Synced: 2025-06-02T01:40:01.654Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 53.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# io-ts-path
[](https://circleci.com/gh/marcello3d/io-ts-path)
[](https://badge.fury.io/js/io-ts-path)
[](https://codecov.io/gh/marcello3d/io-ts-path)Generate type-safe paths from [io-ts](https://github.com/gcanti/io-ts) models.
## Basic usage
### Type-safe path from io-ts type
```typescript
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:
```typescript
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
```typescript
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.numbertype(Person, ['fullName', 'first'])
// => t.stringtype(Person, ['foo'])
// => throws PathError
``````typescript
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)
```