{"id":36790200,"url":"https://github.com/davidmz/ts-json-check","last_synced_at":"2026-01-12T13:24:52.390Z","repository":{"id":35059763,"uuid":"201909433","full_name":"davidmz/ts-json-check","owner":"davidmz","description":"Simple JSON-data typechecker for TypeScript","archived":false,"fork":false,"pushed_at":"2023-03-04T04:30:00.000Z","size":622,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-01T12:49:00.553Z","etag":null,"topics":["json","typechecker","types","typescript","typescript-library","validation"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/davidmz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-12T10:33:33.000Z","updated_at":"2022-02-19T15:20:37.000Z","dependencies_parsed_at":"2023-01-15T12:58:01.481Z","dependency_job_id":null,"html_url":"https://github.com/davidmz/ts-json-check","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/davidmz/ts-json-check","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmz%2Fts-json-check","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmz%2Fts-json-check/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmz%2Fts-json-check/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmz%2Fts-json-check/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidmz","download_url":"https://codeload.github.com/davidmz/ts-json-check/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidmz%2Fts-json-check/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["json","typechecker","types","typescript","typescript-library","validation"],"created_at":"2026-01-12T13:24:52.291Z","updated_at":"2026-01-12T13:24:52.358Z","avatar_url":"https://github.com/davidmz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-json-check\n\nThis library is a simple JSON-data typechecker for TypeScript.\nIt will be useful when you have some arbitrary JSON-data\n(for example, API response) and you need to check them and\ncast them to the right type.\n\n## Install\n\n```\nnpm install ts-json-check\n```\n\nor\n\n```\nyarn add ts-json-check\n```\n\n## Usage\n\n```typescript\nimport {\n  isObject,\n  isBoolean,\n  isNumber,\n  isString,\n  isAnyOf,\n  // ...\n} from \"ts-json-check\";\n\n// ...\n\n// You can't be sure that the data from the server is correct, so\n// apiResponse has an 'any' type.\nconst apiResponse = getSomeData();\n\n// Describe the desired shape of data using ts-json-check\n// guard functions.\nconst isResponse = isObject({\n  id: isAnyOf(isNumber, isString),\n  title: isString,\n  archived: isBoolean,\n});\n\n// To extract the resulting type from the guard use the GuardedType utility\n// type. The APIResponse here is:\n// { id: number|string, title: string, archived: boolean }\ntype APIResponse = GuardedType\u003ctypeof isResponse\u003e;\n\nif (isResponse(apiResponse)) {\n  // The apiResponse have an APIResponse type here\n} else {\n  // apiResponse have a wrong type\n}\n```\n\n## API\n\nThis library is intended only for JSON processing, so it does not attempt\nto simulate all the types available in TypeScript.\n\n### Primitive guards\n\nThe following guards are corresponds to the primitive JSON types:\n\n- **isNull**\n- **isNumber**\n- **isString**\n- **isBoolean**\n\n### Constant guard\n\nThere is one guard that checks that the argument is a constant value (or any of constant values) of\nprimitive JSON type: **isConst**.\n\nUse it as: `isConst(42)` or as `isConst(42, 43)`\n\nIt is useful when your data can have different shape depending on value of some field\n(the _discriminant_ in TS terms).\n\nThe multi-argument form of this guard (`isConst(42, 43)`) is equivalent to the following isAnyOf\nform: `isAnyOf(isConst(42), isConst(43))`.\n\n### 'Any' guard\n\nThere is **isAny** guard that is always returns _true_ and keeps it argument as _any_. It is useful\nwhen you don't know the exact type of your data yet and want to keep some fields untyped.\n\n### Composite guards\n\nThe composite JSON types are expressed by the following functions:\n\n**isObject**\n\nJSON object: `{ \"foo\": 42, \"bar\": \"baz\" }`\n\nUse it as: `isObject({ foo: isNumber, bar: isString})`. You can use any guards\nas the values of the argument object. The input data object can have additional\nkeys, it is not an error.\n\n**isArray**\n\nAn array of values of the same type: `[1, 2, 3, 4]`\n\nUse it as: `isArray(isNumber)`\n\n**isTuple**\n\nAn array of values of different types: `[42, \"baz\"]`. In TypeScript\nthis corresponds to tuples.\n\nUse it as: `isTuple(isNumber, isString)`\n\nThe length of the input data array must be equal to the count\nof the isTuple arguments. The argument list may not be empty.\n\n### Utility checkers\n\n**isAnyOf**\n\nChecks that value have one of the given types.\n\nUse it as: `isAnyOf(isNumber, isString)`\n\nThe resulting type will be `number | string`.\nThe isAnyOf accepts two or more checkers as arguments.\n\n**isOptional**\n\nMarks object field as optional.\n\nUse it as: `isObject({ id: isNumber, title: isOptional(isString) })`\n\nThe resulting type will be `{ id: number, title?: string | undefined }`.\n\n### Custom guards\n\nAlthough this is not usually necessary, you can create your own guard functions.\nThe type is simple: `Guard\u003cT\u003e = (v: any) =\u003e v is T`. Guard function should check\nargument and return _true_ if it has the correct type of _false_ otherwise (see\n[the TypeScript docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)).\n\nFor example let's create guard for positive numbers:\n\n```typescript\nimport { isNumber } from \"ts-json-check\";\n\nfunction isPositiveNumber(v: any): v is number {\n  return isNumber(v) \u0026\u0026 v \u003e 0;\n}\n```\nNote that the guarded type of _isPositiveNumber_ is still a _number_. TypeScript\nhasn't a special type for the positive numbers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidmz%2Fts-json-check","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidmz%2Fts-json-check","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidmz%2Fts-json-check/lists"}