{"id":16293194,"url":"https://github.com/noriste/typescript-is-type","last_synced_at":"2025-03-20T03:31:04.818Z","repository":{"id":38271710,"uuid":"173066823","full_name":"NoriSte/typescript-is-type","owner":"NoriSte","description":"A Typescript-safe runtime type check function","archived":false,"fork":false,"pushed_at":"2023-06-03T10:17:25.000Z","size":654,"stargazers_count":3,"open_issues_count":23,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-14T20:39:57.615Z","etag":null,"topics":["runtime-typechecking","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/typescript-is-type","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NoriSte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-28T07:52:32.000Z","updated_at":"2022-05-18T10:05:30.000Z","dependencies_parsed_at":"2024-10-10T20:10:34.760Z","dependency_job_id":"faa7de87-bac4-4dfb-ae35-2df0541c86f7","html_url":"https://github.com/NoriSte/typescript-is-type","commit_stats":{"total_commits":163,"total_committers":4,"mean_commits":40.75,"dds":0.1165644171779141,"last_synced_commit":"13f6f8036351f9634f97bd64112560725352cc31"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NoriSte%2Ftypescript-is-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NoriSte%2Ftypescript-is-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NoriSte%2Ftypescript-is-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NoriSte%2Ftypescript-is-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NoriSte","download_url":"https://codeload.github.com/NoriSte/typescript-is-type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244047647,"owners_count":20389206,"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":["runtime-typechecking","typescript"],"created_at":"2024-10-10T20:10:30.116Z","updated_at":"2025-03-20T03:31:04.425Z","avatar_url":"https://github.com/NoriSte.png","language":"TypeScript","readme":"# typescript-is-type\n\nA TypeScript-safe runtime type check function\n\n[![Build Status](https://travis-ci.com/NoriSte/typescript-is-type.svg?branch=master)](https://travis-ci.com/NoriSte/typescript-is-type)\n[![Build Cron](https://img.shields.io/badge/build%20cron-weekly-44cc11.svg)](https://travis-ci.com/NoriSte/typescript-is-type)\n[![Coverage Status](https://coveralls.io/repos/github/NoriSte/typescript-is-type/badge.svg)](https://coveralls.io/github/NoriSte/typescript-is-type)\n[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/NoriSte/typescript-is-type/master)](https://stryker-mutator.github.io)\n[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com/)\n[![Maintainability](https://api.codeclimate.com/v1/badges/a6ff58769fe0c4a9e8b9/maintainability)](https://codeclimate.com/github/NoriSte/typescript-is-type/maintainability)\n[![TypeScript](https://badges.frapsoft.com/typescript/love/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/)\n\nNetwork requests responses or JSON based data doesn't allow TypeScript to perform compile-time checks. You can cast the response but it doesn't give you the confidence that the data is an instance of the desired type.\n\nThis simple one-function package allows you to perform both TypeScript-safe and runtime-safe data check.\n\nIf one of the keys to be checked is `undefined` than the check doesn't pass (it's not based on `hasOwnProperty`).\n\n```bash\n# isntall it with\nnpm install --save-dev typescript-is-type\n```\n\n```typescript\nis\u003cstring\u003e(\"Hello world\", \"length\"); // true\nis\u003cstring\u003e(\"Hello world\", \"concat\"); // TS compile error, \"concat\" isn't a key of string\nis\u003cstring\u003e(JSON.parse(JSON.stringify(\"Hello world\")), \"length\"); // true\n```\n\nThat's the function signature\n\n```typescript\nfunction is\u003cType\u003e(instance: any, keys: keyof Type | (keyof Type)[]): instance is Type;\n```\n\nA more explanatory example\n\n```typescript\nimport { is } from \"typescript-is-type\";\n\ninterface Car {\n  power: number;\n}\ninterface FuelCar extends Car {\n  tank: number;\n}\ninterface ElectricCar extends Car {\n  battery: number;\n  singlePedalDrive: boolean;\n}\n\nis\u003cElectricCar\u003e(\n  JSON.parse(\n    JSON.stringify({\n      power: 450,\n      tank: 60\n    })\n  ),\n  \"battery\"\n); // false\n```\n\nRemember that it's up to you to decide the keys to be checked to avoid every false positive/negative.\n\n```typescript\nis\u003cElectricCar\u003e({\n  power: 450,\n  tank: 60\n}), \"power\") // true 🤔\nis\u003cElectricCar\u003e({\n  power: 450,\n  tank: 60\n}), [\"power\", \"battery\"]) // false 🎉\n```\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore --\u003e\n\u003ctable\u003e\u003ctr\u003e\u003ctd align=\"center\"\u003e\u003ca href=\"https://twitter.com/NoriSte\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/173663?v=4\" width=\"100px;\" alt=\"Stefano Magni\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eStefano Magni\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/NoriSte/typescript-is-type/commits?author=NoriSte\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/NoriSte/typescript-is-type/commits?author=NoriSte\" title=\"Tests\"\u003e⚠️\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoriste%2Ftypescript-is-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoriste%2Ftypescript-is-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoriste%2Ftypescript-is-type/lists"}