{"id":23773718,"url":"https://github.com/beenotung/gen-ts-type","last_synced_at":"2025-08-10T16:33:18.723Z","repository":{"id":57245863,"uuid":"188012915","full_name":"beenotung/gen-ts-type","owner":"beenotung","description":"Generate TypeScript types from sample data in browser, node.js, and cli.","archived":false,"fork":false,"pushed_at":"2025-05-17T11:46:50.000Z","size":74,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-09T06:47:01.839Z","etag":null,"topics":["cli","code-generation","reflection","types","typescript"],"latest_commit_sha":null,"homepage":"https://gen-ts-type.surge.sh/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beenotung.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-05-22T10:06:18.000Z","updated_at":"2025-05-21T23:11:03.000Z","dependencies_parsed_at":"2025-01-01T05:42:04.946Z","dependency_job_id":"c9129310-88b8-4710-bfc8-3e42907fe0b4","html_url":"https://github.com/beenotung/gen-ts-type","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/beenotung/gen-ts-type","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fgen-ts-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fgen-ts-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fgen-ts-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fgen-ts-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beenotung","download_url":"https://codeload.github.com/beenotung/gen-ts-type/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fgen-ts-type/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269712877,"owners_count":24463215,"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-08-10T02:00:08.965Z","response_time":71,"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":["cli","code-generation","reflection","types","typescript"],"created_at":"2025-01-01T05:41:59.311Z","updated_at":"2025-08-10T16:33:18.705Z","avatar_url":"https://github.com/beenotung.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gen-ts-type\n\nGenerate TypeScript types from sample data in browser, node.js, and cli.\n\n[![npm Package Version](https://img.shields.io/npm/v/gen-ts-type)](https://www.npmjs.com/package/gen-ts-type)\n\n\u003e 🔥 Use it directly on [Online Playground](https://gen-ts-type.surge.sh)\n\n## Quick Example\n\nFrom CLI:\n\n```bash\n# Generate type from JSON file\necho -n 'export type User = ' \u003e user.d.ts\nnpx -y gen-ts-type user.json \u003e\u003e user.d.ts\n```\n\nFrom TypeScript:\n\n```typescript\nimport { genTsType } from 'gen-ts-type'\nimport { writeFileSync } from 'fs'\nconst data = {\n  name: 'Alice',\n  age: 20,\n  hobbies: ['coding', 'reading'],\n  contact: {\n    email: 'alice@example.com',\n    phone: null,\n  },\n  lastLogin: new Date(),\n}\n\nconst code = genTsType(data, { export: true, name: 'User' })\nwriteFileSync('user.d.ts', code)\n```\n\nGenerated type:\n\n```typescript\nexport type User = {\n  name: string\n  age: number\n  hobbies: Array\u003cstring\u003e\n  contact: {\n    email: string\n    phone: null\n  }\n  lastLogin: Date\n}\n```\n\nSee [Usage](#usage) section below for more options and advanced features.\n\n## Features\n\n### Supported Types\n\n- Primitive Types\n  - string\n  - number\n  - boolean\n  - bigint\n  - Date\n  - symbol\n  - null\n  - undefined\n- Complex Types\n  - Array (single-type and union-type)\n  - Set\n  - Map\n  - Object (with optional properties)\n  - Function\n\n### Type Inference Features\n\n- Detect array of varies object shape, and collapse into optional property or union type\n- Union type support for array elements and object properties\n- Special character handling in object keys\n- Nested object and array support\n- Generic type inference for collections (Array/Set/Map)\n\n## Installation\n\n```bash\n# Install as dev dependency, then use with \"npx gen-ts-type\"\nnpm i -D gen-ts-type\n\n# Or install as global dependency, then use with \"gen-ts-type\"\nnpm i -g gen-ts-type\n```\n\n## Usage\n\n### Usage from CLI\n\n```bash\n# Generate type from stdin\necho -n 'export type PackageJSON = ' \u003e package.d.ts\necho '{\"name\": \"test\", \"version\": \"1.0.0\"}' | npx gen-ts-type \u003e\u003e package.d.ts\n\n# Generate type from a JSON file\nexport=true name=PackageJSON npx gen-ts-type package.json \u003e package.d.ts\n```\n\nGenerated type in file `package.d.ts` will be like:\n\n```typescript\nexport type PackageJSON = {\n  name: string\n  version: string\n}\n```\n\n#### CLI Environment Variables\n\nType Declaration Options:\n\n- `name`: Declare as named type (e.g. `type User = ...`)\n- `export`: Export the type declaration (default: `false`)\n\nFormatting Options:\n\n- `indent`: Initial indent level (default: `''`, i.e. no indent)\n- `indent_step`: Indent step size (default: `'  '`, i.e. 2 spaces)\n- `semi_colon`: Add semicolons after object properties (default: `false`)\n- `include_sample`: Include sample values in comments (default: `false`)\n\nType Inference Options:\n\n- `union_type`: Use union of exact types for array objects instead of optional fields (default: `false`)\n\n### Usage from TypeScript\n\n```typescript\nimport { genTsType } from 'gen-ts-type'\nimport { writeFileSync } from 'fs'\n\nconst type = genTsType(\n  {\n    name: 'Alice',\n    friends: [{ name: 'Bob', since: new Date() }],\n  },\n  {\n    export: true,\n    name: 'User',\n    semi_colon: true,\n  },\n)\n\nwriteFileSync('user.d.ts', type)\n```\n\nGenerated type in file `user.d.ts` will be:\n\n```typescript\nexport type User = {\n  name: string\n  friends: Array\u003c{\n    name: string\n    since: Date\n  }\u003e\n}\n```\n\n#### TypeScript API Options\n\nWhen using the `genTsType` function, you can provide options via an object:\n\n```typescript\ninterface GenTsTypeOptions {\n  // Type Declaration\n  name?: string\n  export?: boolean\n\n  // Formatting\n  indent?: string\n  indent_step?: string\n  semi_colon?: boolean\n  include_sample?: boolean\n\n  // Type Inference\n  union_type?: boolean\n}\n```\n\n### Advanced Usage with Lower-level APIs\n\nMore examples in [examples/advanced-usage.ts](examples/advanced-usage.ts) and [test/ts-type-test.ts](test/ts-type-test.ts)\n\n#### Modify Inferred Type Structure\n\nFor more control over the type generation process, you can use the lower-level `inferType()` function and `Type` class directly:\n\n```typescript\nimport { inferType, Type } from 'gen-ts-type'\n\n// Infer type structure without generating type declaration\nlet userType: Type = inferType({\n  name: 'Alice',\n  friends: [{ name: 'Bob', since: new Date() }],\n})\n\n// Make the User.friends[number].since field optional\n{\n  let friendType = userType.object![1].type.array![0]\n  let sinceType = friendType.object![1].type\n  sinceType.optional = true\n}\n\n// Add User.is_admin field\n{\n  let isAdminType = new Type({ path: '', indent: '  ' })\n  isAdminType.primitive = [true]\n  isAdminType.nullable = true\n  userType.object!.push({\n    key: 'is_admin',\n    type: isAdminType,\n  })\n}\n\n// Generate type declaration with custom options\nlet code = 'export type User = '\ncode += userType.toString({\n  include_sample: true,\n  semi_colon: true,\n})\n\nconsole.log(code)\n```\n\nThe output of above example:\n\n```typescript\nexport type User = {\n  name: string /** e.g. \"Alice\" */\n  friends: Array\u003c{\n    name: string /** e.g. \"Bob\" */\n    since?: Date /** e.g. \"2025-05-17T08:35:10.429Z\" */\n  }\u003e\n  is_admin: null | boolean /** e.g. true */\n}\n```\n\nThe Type class provides access to the inferred type structure:\n\n- `type.object`: Array of object fields `{ key: string, type: Type }`\n- `type.array`: Array of element types\n- `type.primitive`: Array of primitive values\n- `type.nullable`: Whether the type includes null\n- `type.optional`: Whether the type includes undefined\n- `type.toString(options)`: Generate type declaration string\n\n#### Example with union_type\n\n```typescript\n// Data:\nconst data = [\n  { name: 'Alice', year: 2000 },\n  { name: 'Bob', age: 20 },\n]\n\n// With union_type=false (default):\ntype Data = Array\u003c{\n  name: string\n  year?: number\n  age?: number\n}\u003e\n\n// With union_type=true:\ntype Data = Array\u003c\n  | {\n      name: string\n      year: number\n    }\n  | {\n      name: string\n      age: number\n    }\n\u003e\n```\n\n## License\n\nThis project is licensed with [BSD-2-Clause](./LICENSE)\n\nThis is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):\n\n- The freedom to run the program as you wish, for any purpose\n- The freedom to study how the program works, and change it so it does your computing as you wish\n- The freedom to redistribute copies so you can help others\n- The freedom to distribute copies of your modified versions to others\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fgen-ts-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeenotung%2Fgen-ts-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fgen-ts-type/lists"}