# typio

> Util for casting any object to specified model with typescript type inference

Latest version **0.3.9** (published 2020-06-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install typio
pnpm add typio
yarn add typio
bun add typio
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.9 |
| Published | 2020-06-17 |
| First published | 2018-06-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 150.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | arvitaly |
| Maintainers | arvitaly |
| Keywords | types, typescript, cast, interfaces, check |

## Links

- npm: https://www.npmjs.com/package/typio
- Repository: https://github.com/arvitaly/typio
- Homepage: https://github.com/arvitaly/typio#readme
- Issues: https://github.com/arvitaly/typio/issues
- npm.io page: https://npm.io/package/typio

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.3.9 (latest) — 2020-06-17
- 0.3.8 — 2020-04-18
- 0.3.7 — 2019-09-25
- 0.3.6 — 2019-06-15
- 0.3.5 — 2019-05-10
- 0.3.4 — 2019-05-07
- 0.3.3 — 2019-04-22
- 0.3.2 — 2019-04-21
- 0.3.1 — 2019-04-21
- 0.3.0 — 2019-04-21
- 0.2.6 — 2018-12-16
- 0.2.5 — 2018-12-16
- 0.2.4 — 2018-12-16
- 0.2.3 — 2018-11-19
- 0.2.2 — 2018-07-08
- … 5 more at https://npm.io/package/typio/versions

## README

# typio

Util for casting any object to specified model with typescript type inference

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

![alt text](typio.png "Demo")

# Install

    npm install typio --save

    or

    yarn add typio

# Usage

```typescript
import { date, enume, float, max, min, num, opt, or, str, typio } from "typio";

enum TestEnum {
  EnumValue1 = "EnumValue1",
  EnumValue2 = "EnumValue2",
}
// any date
const dt1 = new Date("2011-01-02 00:00:00");
// create raw plain-object
const raw = JSON.parse(
  JSON.stringify({
    bool1: true,
    str1: 20,
    int1: "30",
    num1: "1.45",
    obj1: {
      enum1: "EnumValue1",
    },
    opt2: "-100.5",
    date1: dt1.toString(),
    arr1: ["str1", "15"],
    arr2: ["str2", "str3"],
    or1: "Hello",
  }),
);
// call typio with raw-object and model
const result = typio(raw, {
  bool1: bool(),
  str1: str(min(1), max(15)),
  int1: int(min(15), max(45)),
  num1: num(max(15)),
  obj1: {
    enum1: enume(TestEnum),
  },
  opt1: opt(str()),
  opt2: opt(float()),
  date1: date(),
  arr1: [str(), num()], // tuple
  arr2: [str()],
  or1: or(bool(), str(), num()),
});
// check types, all types cast to model, if they can
expect(result.bool1).toBe(true);
expect(result.str1.substr(0, 2)).toBe("20");
expect(result.int1).toBe(30);
expect(result.num1.toExponential()).toBe("1.45e+0");
expect(result.obj1.enum1).toBe(TestEnum.EnumValue1);
expect(result.opt1).toBe(undefined);
expect(result.opt2).toBe(-100.5);
expect(result.date1.getTime()).toBe(dt1.getTime());
expect(result.arr1).toEqual(["str1", 15]);
expect(result.arr2).toEqual(["str2", "str3"]);
expect(result.or1).toBe("Hello");
```

# API

```typescript
function typio<T extends any>(obj: any, model: T): T;

// operator return true or false or error-string (equal false)
type TypioOperator<T> = ((value: T) => true | false | string) & {
  label: string;
};

type CastResult<T> =
  | { type: "error"; error?: string; operator?: string; value: T }
  | { type: "success"; value: T };
```

## Types

```typescript
function bool(...operators: Array<TypioOperator<boolean>>): boolean;
function date(...operators: Array<TypioOperator<Date>>): Date;
function enume<T>(
  enumerate: T,
  ...operators: Array<TypioOperator<keyof T>>
): T[keyof T];
function float(...operators: Array<TypioOperator<number>>): number;
function int(...operators: Array<TypioOperator<number>>): number;
function num(...operators: Array<TypioOperator<number>>): number;
function opt<T>(obj: T): T | undefined;
function or<T extends any[]>(objs: T): T[number];
function str(...operators: Array<TypioOperator<string>>): string;
```

## Operators

```typescript

// MAX
interface MaxOperator {
    (maxValue: number): TypioOperator<number | string | any[]>;
}
interface MaxOperator {
    (maxValue: Date): TypioOperator<Date>;
}

function max: MaxOperator;

// MIN
interface MinOperator {
    (minValue: number): TypioOperator<number | string | any[]>;
}
interface MinOperator {
    (minValue: Date): TypioOperator<Date>;
}
function min: MinOperator;

// MATCH
interface MatchOperator {
    (regexp: RegExp): TypioOperator<string>;
}

function match: MatchOperator;
```

# Test

    npm install
    npm test

[npm-image]: https://badge.fury.io/js/typio.svg
[npm-url]: https://npmjs.org/package/typio
[travis-image]: https://travis-ci.org/arvitaly/typio.svg?branch=master
[travis-url]: https://travis-ci.org/arvitaly/typio
[daviddm-image]: https://david-dm.org/arvitaly/typio.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/arvitaly/typio
[coveralls-image]: https://coveralls.io/repos/arvitaly/typio/badge.svg
[coveralls-url]: https://coveralls.io/r/arvitaly/typio

---
_Source: https://npm.io/package/typio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
