{"id":25938613,"url":"https://github.com/michaeljscript/create-typeguard","last_synced_at":"2025-03-04T03:37:41.428Z","repository":{"id":34444489,"uuid":"179064013","full_name":"michaeljscript/create-typeguard","owner":"michaeljscript","description":"Simple package for creating safe type guards for TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-03T18:58:57.000Z","size":612,"stargazers_count":14,"open_issues_count":14,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-11T05:17:02.836Z","etag":null,"topics":["typeguard","typescript"],"latest_commit_sha":null,"homepage":"https://medium.com/@michalszorad/typescript-keeping-type-guards-safe-and-up-to-date-2457d52bd722","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/michaeljscript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-02T11:33:08.000Z","updated_at":"2023-02-14T14:50:49.000Z","dependencies_parsed_at":"2023-01-15T07:06:29.065Z","dependency_job_id":null,"html_url":"https://github.com/michaeljscript/create-typeguard","commit_stats":null,"previous_names":["michalszorad/create-typeguard"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeljscript%2Fcreate-typeguard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeljscript%2Fcreate-typeguard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeljscript%2Fcreate-typeguard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeljscript%2Fcreate-typeguard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michaeljscript","download_url":"https://codeload.github.com/michaeljscript/create-typeguard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241780475,"owners_count":20019058,"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":["typeguard","typescript"],"created_at":"2025-03-04T03:37:40.560Z","updated_at":"2025-03-04T03:37:41.415Z","avatar_url":"https://github.com/michaeljscript.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Create Type Guard\n\nSimple package for creating type-safe type guards for TypeScript.\n\n## What problem is being solved?\n\n```ts\ninterface Person {\n  age: number;\n  name: string;\n}\n\nfunction isPerson(data: unknown): data is Person {\n  return typeof data === 'object' \u0026\u0026 data \u0026\u0026 Object.hasOwnProperty.call(data, 'name');\n}\n\n// Oooops, you forgot to check if the `age` property does exist in `data`.\n// Oooops, you forgot to check the type of the `name` property. Is it a string? Is it a number?\n// Oooops, you forgot to check the type of the `age` property. Is it a string? Is it a number?\n\n// With create-typeguard this won't happen.\n```\n\n## How does it work?\nThis package creates type-safe type guards from a parsers, because parsers will warn us if we make a mistake!\n\n## Usage\n\n```ts\nimport { createTypeGuard, hasProperties } from \"create-typeguard\";\n\ninterface Person {\n  age: number;\n  name: string;\n}\n\n// Our parser function that is type-safe\nfunction parsePerson(data: unknown): Person | null {\n\n  if (typeof data === 'object' \u0026\u0026 data \u0026\u0026 hasProperties(data, 'name', 'age')) {\n    const { name, age } = data;\n\n    if (typeof name === 'string' \u0026\u0026 typeof age === 'number') {\n      return { name, age };\n    }\n  }\n\n  return null;\n}\n\n// This is now a type-safe typeguard for Person!\nconst isPerson = createTypeGuard(parsePerson);\n\n\nconst maybePerson: unknown = response.body;\n\nif (isPerson(maybePerson)) {\n    // maybePerson is Person\n    console.log(`${maybePerson.name} is ${maybePerson.age} years old.`);\n}\n\n```\n\n## Usage 2 - Without defining a separate parse function \n\n```ts\nimport { createTypeGuard, hasProperties } from \"create-typeguard\";\n\ninterface Person {\n  age: number;\n  name: string;\n}\n\n// You do not have to create a separate parse function.\nconst isPerson = createTypeGuard\u003cPerson\u003e(data =\u003e {\n  if (typeof data === 'object' \u0026\u0026 data \u0026\u0026 hasProperties(data, 'name', 'age')) {\n    const { name, age } = data;\n\n    if (typeof name === 'string' \u0026\u0026 typeof age === 'number') {\n      return { name, age };\n    }\n  }\n  \n  return null;\n});\n\n\nconst maybePerson: unknown = response.body;\n\nif (isPerson(maybePerson)) {\n    // maybePerson is Person\n    console.log(`${maybePerson.name} is ${maybePerson.age} years old.`);\n}\n\n```\n\n## Why create-typeguard?\nRead more at this blog post https://medium.com/@michalszorad/typescript-keeping-type-guards-safe-and-up-to-date-2457d52bd722\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaeljscript%2Fcreate-typeguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaeljscript%2Fcreate-typeguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaeljscript%2Fcreate-typeguard/lists"}