{"id":20432032,"url":"https://github.com/arvitaly/typio","last_synced_at":"2025-07-14T00:38:42.506Z","repository":{"id":57384135,"uuid":"137798137","full_name":"arvitaly/typio","owner":"arvitaly","description":"Util for casting any object to specified model with typescript type inference","archived":false,"fork":false,"pushed_at":"2020-06-17T23:42:00.000Z","size":405,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T20:52:35.949Z","etag":null,"topics":["cast","check","inference","types","typescript","validation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arvitaly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"arvitaly"}},"created_at":"2018-06-18T19:39:36.000Z","updated_at":"2025-02-10T15:52:27.000Z","dependencies_parsed_at":"2022-09-26T16:50:25.761Z","dependency_job_id":null,"html_url":"https://github.com/arvitaly/typio","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/arvitaly/typio","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvitaly%2Ftypio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvitaly%2Ftypio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvitaly%2Ftypio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvitaly%2Ftypio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arvitaly","download_url":"https://codeload.github.com/arvitaly/typio/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvitaly%2Ftypio/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265228227,"owners_count":23731068,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cast","check","inference","types","typescript","validation"],"created_at":"2024-11-15T08:13:43.625Z","updated_at":"2025-07-14T00:38:42.476Z","avatar_url":"https://github.com/arvitaly.png","language":"JavaScript","funding_links":["https://github.com/sponsors/arvitaly"],"categories":[],"sub_categories":[],"readme":"# typio\n\nUtil for casting any object to specified model with typescript type inference\n\n[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]\n[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)\n\n![alt text](typio.png \"Demo\")\n\n# Install\n\n    npm install typio --save\n\n    or\n\n    yarn add typio\n\n# Usage\n\n```typescript\nimport { date, enume, float, max, min, num, opt, or, str, typio } from \"typio\";\n\nenum TestEnum {\n  EnumValue1 = \"EnumValue1\",\n  EnumValue2 = \"EnumValue2\",\n}\n// any date\nconst dt1 = new Date(\"2011-01-02 00:00:00\");\n// create raw plain-object\nconst raw = JSON.parse(\n  JSON.stringify({\n    bool1: true,\n    str1: 20,\n    int1: \"30\",\n    num1: \"1.45\",\n    obj1: {\n      enum1: \"EnumValue1\",\n    },\n    opt2: \"-100.5\",\n    date1: dt1.toString(),\n    arr1: [\"str1\", \"15\"],\n    arr2: [\"str2\", \"str3\"],\n    or1: \"Hello\",\n  }),\n);\n// call typio with raw-object and model\nconst result = typio(raw, {\n  bool1: bool(),\n  str1: str(min(1), max(15)),\n  int1: int(min(15), max(45)),\n  num1: num(max(15)),\n  obj1: {\n    enum1: enume(TestEnum),\n  },\n  opt1: opt(str()),\n  opt2: opt(float()),\n  date1: date(),\n  arr1: [str(), num()], // tuple\n  arr2: [str()],\n  or1: or(bool(), str(), num()),\n});\n// check types, all types cast to model, if they can\nexpect(result.bool1).toBe(true);\nexpect(result.str1.substr(0, 2)).toBe(\"20\");\nexpect(result.int1).toBe(30);\nexpect(result.num1.toExponential()).toBe(\"1.45e+0\");\nexpect(result.obj1.enum1).toBe(TestEnum.EnumValue1);\nexpect(result.opt1).toBe(undefined);\nexpect(result.opt2).toBe(-100.5);\nexpect(result.date1.getTime()).toBe(dt1.getTime());\nexpect(result.arr1).toEqual([\"str1\", 15]);\nexpect(result.arr2).toEqual([\"str2\", \"str3\"]);\nexpect(result.or1).toBe(\"Hello\");\n```\n\n# API\n\n```typescript\nfunction typio\u003cT extends any\u003e(obj: any, model: T): T;\n\n// operator return true or false or error-string (equal false)\ntype TypioOperator\u003cT\u003e = ((value: T) =\u003e true | false | string) \u0026 {\n  label: string;\n};\n\ntype CastResult\u003cT\u003e =\n  | { type: \"error\"; error?: string; operator?: string; value: T }\n  | { type: \"success\"; value: T };\n```\n\n## Types\n\n```typescript\nfunction bool(...operators: Array\u003cTypioOperator\u003cboolean\u003e\u003e): boolean;\nfunction date(...operators: Array\u003cTypioOperator\u003cDate\u003e\u003e): Date;\nfunction enume\u003cT\u003e(\n  enumerate: T,\n  ...operators: Array\u003cTypioOperator\u003ckeyof T\u003e\u003e\n): T[keyof T];\nfunction float(...operators: Array\u003cTypioOperator\u003cnumber\u003e\u003e): number;\nfunction int(...operators: Array\u003cTypioOperator\u003cnumber\u003e\u003e): number;\nfunction num(...operators: Array\u003cTypioOperator\u003cnumber\u003e\u003e): number;\nfunction opt\u003cT\u003e(obj: T): T | undefined;\nfunction or\u003cT extends any[]\u003e(objs: T): T[number];\nfunction str(...operators: Array\u003cTypioOperator\u003cstring\u003e\u003e): string;\n```\n\n## Operators\n\n```typescript\n\n// MAX\ninterface MaxOperator {\n    (maxValue: number): TypioOperator\u003cnumber | string | any[]\u003e;\n}\ninterface MaxOperator {\n    (maxValue: Date): TypioOperator\u003cDate\u003e;\n}\n\nfunction max: MaxOperator;\n\n// MIN\ninterface MinOperator {\n    (minValue: number): TypioOperator\u003cnumber | string | any[]\u003e;\n}\ninterface MinOperator {\n    (minValue: Date): TypioOperator\u003cDate\u003e;\n}\nfunction min: MinOperator;\n\n// MATCH\ninterface MatchOperator {\n    (regexp: RegExp): TypioOperator\u003cstring\u003e;\n}\n\nfunction match: MatchOperator;\n```\n\n# Test\n\n    npm install\n    npm test\n\n[npm-image]: https://badge.fury.io/js/typio.svg\n[npm-url]: https://npmjs.org/package/typio\n[travis-image]: https://travis-ci.org/arvitaly/typio.svg?branch=master\n[travis-url]: https://travis-ci.org/arvitaly/typio\n[daviddm-image]: https://david-dm.org/arvitaly/typio.svg?theme=shields.io\n[daviddm-url]: https://david-dm.org/arvitaly/typio\n[coveralls-image]: https://coveralls.io/repos/arvitaly/typio/badge.svg\n[coveralls-url]: https://coveralls.io/r/arvitaly/typio\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farvitaly%2Ftypio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farvitaly%2Ftypio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farvitaly%2Ftypio/lists"}