{"id":20665386,"url":"https://github.com/rohit1901/ts-gen-typeguards","last_synced_at":"2025-12-31T00:38:30.661Z","repository":{"id":180241176,"uuid":"664820593","full_name":"rohit1901/ts-gen-typeguards","owner":"rohit1901","description":"A Typescript Library which generates type guard functions. #library","archived":false,"fork":false,"pushed_at":"2023-09-04T14:28:51.000Z","size":834,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-15T03:35:57.762Z","etag":null,"topics":["jest","library","nodejs","npm","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ts-gen-typeguards","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/rohit1901.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2023-07-10T20:29:45.000Z","updated_at":"2025-08-25T16:55:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"f5a92d4e-6b0e-4e42-aca8-5c072b9d0dbe","html_url":"https://github.com/rohit1901/ts-gen-typeguards","commit_stats":null,"previous_names":["rohit1901/ts-gen-typeguards"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/rohit1901/ts-gen-typeguards","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohit1901%2Fts-gen-typeguards","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohit1901%2Fts-gen-typeguards/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohit1901%2Fts-gen-typeguards/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohit1901%2Fts-gen-typeguards/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rohit1901","download_url":"https://codeload.github.com/rohit1901/ts-gen-typeguards/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohit1901%2Fts-gen-typeguards/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28017789,"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","status":"online","status_checked_at":"2025-12-25T02:00:05.988Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["jest","library","nodejs","npm","typescript"],"created_at":"2024-11-16T19:30:38.247Z","updated_at":"2025-12-25T03:10:03.175Z","avatar_url":"https://github.com/rohit1901.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-gen-typeguards\n![Typescript](https://shields.io/badge/TypeScript-3178C6?logo=TypeScript\u0026logoColor=FFF\u0026style=flat-square)\n\n[![npm](https://img.shields.io/npm/v/ts-gen-typeguards)](https://www.npmjs.com/package/ts-gen-typeguards)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n![Build Status](https://github.com/rohit1901/ts-gen-typeguards/actions/workflows/ci.yml/badge.svg)\n\n\u003e A TypeScript project that provides utility functions to generate type guards for interfaces, type aliases, and enums.\n\n## Description\n\nThe Type Guards Generator is a utility for automatically generating TypeScript type guards based on interfaces, types, and enums defined in your code. This tool simplifies the process of ensuring type safety and data validation in your TypeScript projects.\n\n## Installation\n```bash\nnpm install ts-gen-typeguards\n```\n### Usage\n\nYou can use the Type Guards Generator in various ways depending on your requirements. Here are a few usage examples:\n\n1. Using default values:\n   ```typescript\n   tsGenTypeguards();\n   ```\n\n2. Using custom values for input and output paths:\n   ```typescript\n   tsGenTypeguards(undefined, 'inputNew', 'outputNew');\n   ```\n\n3. Using custom input and default output paths:\n   ```typescript\n   tsGenTypeguards(undefined, 'inputNew');\n   ```\n\n4. Using default input and custom output paths:\n   ```typescript\n   tsGenTypeguards(undefined, undefined, 'outputNew');\n   ```\n\n5. Using an input string and default output paths:\n   ```typescript\n   tsGenTypeguards('export interface User {name: string; age: number;}');\n   ```\n\n### Getting Started\n\nTo get started, follow these steps:\n\n1. Install the Type Guards Generator as described in the **Installation** section.\n2. Modify the generator's input files or provide input strings as needed.\n3. Run your TypeScript code to generate type guards automatically.\n\nFor detailed information on how to use and customize the Type Guards Generator, refer to the code comments and documentation.\n## Suppported combinations\nThe provided functions in the `typeGuardsGenerator` module support generating type guards for various combinations of TypeScript types, interfaces, and enums. Here's a summary of the supported combinations:\n\n1. **Interfaces**:\n    - Type guards can be generated for interfaces that contain property signatures with different data types (e.g., string, number, boolean, etc.).\n    - Interfaces can include nested types like arrays, unions, intersections, and conditional types, and the type guards will handle them accordingly.\n\n2. **Type Aliases**:\n    - Type guards can be generated for type aliases with different data types.\n    - Type aliases can include nested types like arrays, unions, intersections, and conditional types, and the type guards will handle them accordingly.\n\n3. **Enums**:\n    - Type guards can be generated for enums with string or numeric values.\n\n4. **Union Types**:\n    - The utility will correctly handle type guards for union types containing multiple data types (e.g., string | number | boolean).\n\n5. **Intersection Types**:\n    - The utility will correctly handle type guards for intersection types containing multiple interfaces or type aliases.\n\n6. **Conditional Types**:\n    - The utility will correctly handle type guards for conditional types, including the true and false branches.\n\n7. **Literal Types**:\n    - The utility will correctly handle type guards for literal types, such as string literals, numeric literals, boolean literals, null, and undefined.\n8. **Optional Properties**:\n    - The utility will correctly handle type guards for optional properties.\n9. **Recursive Types**:\n    - The utility will correctly handle type guards for recursive types.\n10. **Readonly Properties**:\n    - The utility will correctly handle type guards for ``readonly`` properties.\n11. **Any and Unknown Types**:\n    - The utility will correctly handle type guards for ``any`` and ``unknown`` types.\n12. **Generic Types**:\n    - The utility will correctly handle type guards for generic types as well as nested generic types as long as the generic type is not a conditional type.\n\nIn summary, the functions are designed to handle a wide range of type combinations and generate accurate type guards to validate data at runtime. The utility aims to provide comprehensive type safety for your TypeScript code by ensuring that the runtime data adheres to the defined TypeScript types.\nTo generate type guards for interfaces, type aliases, and enums, you can utilize the provided functions `generateTypeGuards`, `generateTypeTypeGuards`, and `generateEnumTypeGuard`, respectively.\n\n## Coming soon: \n- Support for generating type guards for conditional types.\n- Support for generating type guards for Indexed Access types.\n- Support for generating type guards for imported Interfaces/Types/Enums.\n- Support for generating type guards for classes.\n- Support for generating type guards for Mapped Types.\n- Support for generating type guards for Function Signatures.\n- Support for generating type guards for Optional Chaining and Nullish Coalescing.\n- Support for generating type guards for Type Assertions.\n- CLI to generate type guards for a given TypeScript file.\n## API Reference\n\nThis function takes a TypeNode representing an intersection type, the name of the type,\nand an optional boolean flag to indicate if the type is a property. It generates and\nreturns an array of type guard code snippets for the provided intersection type.\n### generateIntersectionTypeGuardForType({ type, name }: TypeAliasDeclaration, typeAliases: TypeAliasDeclaration[],)\nGenerate an intersection type guard for a given TypeScript type alias.\n### generateKeywordGuard(type: TypeNode, typeName?: string, isProperty?: boolean,)\nGenerates type guards for the given TypeScript TypeNode, which can be a keyword or a literal Type.\nIf the property propertyName is not provided, the type looks as follows:\n```\nexport type Person4 = number;\n```\n### generateLiteralTypeTypeGuard(literalType: Node)\nGenerates a type guard condition for a literal type node.\n### generateOptionalPropertyTypeGuard(property: PropertySignature, parentName?: string,)\nGenerates Typeguards for an Optional property which could be of the following types:\n- LiteralType\n- TypeReference\n- UnionType\n- IntersectionType\n- KeywordType\n- ArrayType\n- TupleType\n- TypeLiteral\n### generatePropertyGuard(property: TypeElement, parentName?: string,)\nFunction to generate a type guard for a TypeElement. Used to generate type guard string for properties.\nA property (TypeElement) can be either a required or optional property.\nA property could be a TypeLiteral, a TypeReference, an IntersectionType, or a UnionType.\nIf the property is optional, the type guard will be generated using the generateOptionalPropertyTypeGuard function.\nIf the property is required, the type guard will be generated using the generateTypeLiteralTypeGuard function.\n### generateQualifiedNameTypeGuard(type: QualifiedName, typeName?: string,)\nFunction to generate a type guard for a QualifiedName.\nA QualifiedName is used to represent a qualified name, e.g. `A.B` in the following code:\n```\nnamespace A {\n export const B = 1;\n export const C = B;\n export const D = A.B;\n export const E = A.C;\n export const F = A.D;\n export const G = A.E;\n export const H = A.F;\n export const I = A.G;\n }\n ```\nwhere `A.B` is a QualifiedName.\n### generatePropertyTypeGuard({ questionToken, name, type }: PropertySignature, typeAliases: TypeAliasDeclaration[],)\nGenerates a type guard for a property based on its TypeScript PropertySignature.\n### generateTypeReferenceGuard(type: TypeNode, typeName: string, isProperty?: boolean,)\nGenerates a type guard for a TypeReferenceNode. Used to generate type guard string for type aliases.\n```\n// For type alias `Person` with property `name`\nconst typeNode = factory.createTypeReferenceNode('Person', []);\nconst typeName = 'name';\nconst typeGuards = generateTypeReferenceGuard(typeNode, typeName, true);\n// Result: ['isPerson(value.name)']\n// For type alias `Point`\nconst typeNode = factory.createTypeReferenceNode('Point', []);\nconst typeName = 'point';\nconst typeGuards = generateTypeReferenceGuard(typeNode, typeName);\n// Result: ['isPoint(value)']\n```\n### generateTypeTypeGuard(definitions: TypeAliasDeclaration[], enums: EnumDeclaration[],)\nGenerate a set of type guard functions based on provided TypeAliasDeclarations.\n### generateUnionTypeGuard(type: TypeNode, typeName: string, isProperty?: boolean, definitions?: TypeAliasDeclaration[],)\nGenerates a type guard for a union type.\n### generateUnionTypeGuardForIntersection({ type }: TypeAliasDeclaration, typeAliases: TypeAliasDeclaration[], name?: PropertyName,)\nGenerate a union type guard for a given TypeScript type alias.\n### generateGenericPropertyGuard(property: TypeElement, parentName?: string,)\nGenerate a generic type guard for a given property.\n```typescript\nexport function isType\u003cT\u003e(value: any, guard: (val: any) =\u003e val is T): value is Type\u003cT\u003e{return(typeof value === \"object\" \u0026\u0026 value !== null\n\u0026\u0026 guard(value));}\n//for a property name\n   property.hasOwnProperty('name') \u0026\u0026 guard(value.name)\n//for a nested property name\n   property.hasOwnProperty('name') \u0026\u0026 property.name.hasOwnProperty('nested') \u0026\u0026 guard(value.name.nested)\n//for a type Type\u003cT\u003e\n```\n## Usage Examples\n\n```typescript\nimport{ tsGenTypeguards } from 'ts-gen-typeguards/lib';\n// Usage Examples\n// using default values \ntsGenTypeguards();\n// using custom values\ntsGenTypeguards(undefined, 'inputNew', 'outputNew');\n// using custom input and default output values\ntsGenTypeguards(undefined, 'inputNew');\n// using default input and custom output values \ntsGenTypeguards(undefined, undefined, 'outputNew');\n// using custom input string and default output values\ntsGenTypeguards('export interface User {name: string; age: number;}');\n```\n\nThe above code generates type guards for the provided interface declaration and creates a file typeguards.ts with the generated code.\n\n## Contributing\n\nWe welcome contributions from the community! If you'd like to contribute to `ts-gen-typeguards`, please take a moment to read the [Contributing Guidelines](CONTRIBUTING.md) to understand our development process and how to get started.\n\n## Code of Conduct\n\nPlease review the [Code of Conduct](CODE_OF_CONDUCT.md) to understand the behavior we expect from our contributors.\n\n## Changelog\n\nFor information about the latest changes and updates, refer to the [Changelog](CHANGELOG.md).\n\n## License\n\nThis project is licensed under the [MIT License](https://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohit1901%2Fts-gen-typeguards","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frohit1901%2Fts-gen-typeguards","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohit1901%2Fts-gen-typeguards/lists"}