{"id":13528294,"url":"https://github.com/mistlog/typetype","last_synced_at":"2025-04-05T13:01:41.152Z","repository":{"id":38418608,"uuid":"338747538","full_name":"mistlog/typetype","owner":"mistlog","description":"A programming language designed for typescript type generation","archived":false,"fork":false,"pushed_at":"2022-06-04T11:22:44.000Z","size":817,"stargazers_count":419,"open_issues_count":2,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T12:08:14.033Z","etag":null,"topics":["type","type-system","types","typescript"],"latest_commit_sha":null,"homepage":"https://mistlog.github.io/typetype-playground/","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/mistlog.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":"2021-02-14T06:55:46.000Z","updated_at":"2025-03-27T12:54:03.000Z","dependencies_parsed_at":"2022-09-01T10:20:30.848Z","dependency_job_id":null,"html_url":"https://github.com/mistlog/typetype","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Ftypetype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Ftypetype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Ftypetype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Ftypetype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mistlog","download_url":"https://codeload.github.com/mistlog/typetype/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339145,"owners_count":20923012,"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":["type","type-system","types","typescript"],"created_at":"2024-08-01T06:02:24.108Z","updated_at":"2025-04-05T13:01:41.040Z","avatar_url":"https://github.com/mistlog.png","language":"TypeScript","readme":"# TypeType \u0026middot; [![Build Status](https://github.com/mistlog/typetype/workflows/build/badge.svg)](https://github.com/mistlog/typetype/workflows/build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/mistlog/typetype/badge.svg)](https://coveralls.io/github/mistlog/typetype)\n\nTypeType is designed to generate complex typescript type with ease.\n\n* playground: https://mistlog.github.io/typetype-playground/\n* introduction: [The Art of Type Programming](https://itnext.io/the-art-of-type-programming-cfd933bdfff7?source=friends_link\u0026sk=8dfd5a2b491beb9e12990d815b9267ce)\n* quick start: [typetype template](https://github.com/mistlog/typetype-template)\n\n## Usage\n\n```bash\n\u003e npm i -D @mistlog/typetype\n```\n\n### CLI\n\n\u003e example: [typetype-examples/package.json](https://github.com/mistlog/typetype-examples/blob/main/package.json)\n\n```bash\ntypetype build \u003cdir\u003e: build all *.type files in \u003cdir\u003e\ntypetype build -w \u003cdir\u003e: watch all *.type files in \u003cdir\u003e\ntypetype clean \u003cdir\u003e: remove all generated *.ts files in \u003cdir\u003e\ntypetype debug \u003cfile\u003e: build \u003cfile\u003e in debug mode(backtrace will be available)\n```\n\n### API\n\n\u003e example: [typetype-examples/index.ts](https://github.com/mistlog/typetype-examples/blob/main/index.ts)\n\n```ts\nimport { transform } from \"@mistlog/typetype\";\n\nconst input = `\n    type function TypeName = (T) =\u003e ^{\n        if(T extends string) {\n            return \"string\"\n        } else {\n            return \"number\"\n        }\n    }\n`;\nconst output = transform(input).code;\nconsole.log(output);\n// output: type TypeName\u003cT\u003e = T extends string ? \"string\" : \"number\";\n```\n\nDebug mode:\n\n```ts\nconst output = transform(input, { debug: true }).code;\n```\n\nwhen `debug` is true, backtrace will be available:\n\n```log\nExpected end of input but \";\" found.\nx 1:11-1:11 MultiLineComment\n| type a = 1;\n|           ^\no 1:11-1:11 _\n| type a = 1;\n|           ^\nx 1:11-1:11 TypeFunctionDeclaration\n| type a = 1;\n...\n|/ /\n| |\n|/\no 1:1-1:11 TypeFile\n  type a = 1;\n```\n\n## Examples\n\n- all examples: https://github.com/mistlog/typetype-examples\n\nIn the [url-parser](https://github.com/mistlog/typetype-examples/blob/main/examples/url-parser/url-parser.type) example, `function parseURL` will be translated to generic type `parseURL\u003ctext\u003e` in typescript:\n\n```ts\n// input\ntype function parseURL = (text) =\u003e ^{\n    if (parseProtocol\u003ctext\u003e extends [infer protocol, infer rest]) {\n        return {\n            protocol,\n            rest\n        }\n    } else {\n        return never\n    }\n}\n```\n\n```ts\n// output\ntype parseURL\u003ctext\u003e = parseProtocol\u003ctext\u003e extends [infer protocol, infer rest]\n  ? {\n      protocol: protocol;\n      rest: rest;\n    }\n  : never;\n```\n\nConditional type is presented in this way:\n\n```ts\n^{ if ... else ...}\n```\n\nIt can be nested so that the logic is clear:\n\n```ts\ntype function _isNumberString = (text) =\u003e ^{\n    if(text extends \"\") {\n        return true\n    } else if(text extends `${infer digit}${infer rest}`) {\n        return ^{\n            if(digit extends Digit) {\n                return _isNumberString\u003crest\u003e\n            } else {\n                return false\n            }\n        }\n    } else {\n        return false\n    }\n}\n```\n\n- type query examples: [Type Query: jQuery Style Type Manipulation](https://itnext.io/type-query-jquery-style-type-manipulation-497ce26d93f?source=friends_link\u0026sk=0384b7842e0f51940023a0469359a7af)\n\nwe can use js to create types:\n\n```ts\ntype tuple = [\"tesla\", \"model 3\", \"model X\", \"model Y\"]\n\ntype result = ''' \"use js\"\n   return $.use(\"tuple\")\n      .tupleToObject()\n      .omit(key =\u003e !key.startsWith(\"model\"))\n      .type();\n'''\n```\n\ngenerated: \n\n```ts\ntype tuple = [\"tesla\", \"model 3\", \"model X\", \"model Y\"];\ntype result = {\n  \"model 3\": \"model 3\";\n  \"model X\": \"model X\";\n  \"model Y\": \"model Y\";\n};\n```\n\n## Syntax\n\n* visit playground: https://mistlog.github.io/typetype-playground/\n    * or [examples/syntax](https://github.com/mistlog/typetype-examples/blob/main/examples/syntax/syntax.type)\n\n### Basic type\n\n```ts\ntype a = never\ntype b = number\ntype c = string\n```\n\n```ts\ntype value = 1\ntype bool = true\ntype tuple = [1, 2, 3]\ntype array = string[][]\n\ntype str = \"abc\"\ntype template = `value is: ${value}`\n\ntype obj = { a: 1, b: \"abc\", c: [1, 2] }\ntype valueDeep = obj[\"c\"][1]\n\ntype keys = keyof { readonly a?: 1, b: 2 }\n```\n\n### Union and Intersection\n\nWe use `union [...]` or `| [...]` to denote union type.\n\n```ts\ntype u1 = union [0, 1, 2]\ntype u2 = | [0, 1, 2]\n``` \n\nBecause [an intersection type combines multiple types into one](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html?ref=hackernoon.com#intersection-types), we use `combine [...]` or `\u0026 [...]` for intersection type:\n\n```ts\ntype i1 = combine [{ a: 1 }, { b: 2 }]\ntype i2 = \u0026 [{ a: 1 }, { b: 2 }]\n```\n\n### Function type\n\n```ts\ntype f1 = type () =\u003e void\ntype f2 = type (a:number, b:string) =\u003e number\ntype f3 = type () =\u003e type (a:number, b:string) =\u003e void\n```\n\n### Conditional type\n\n```ts\n/*\n  type conditional = 1 extends string ? \"string\" : \"number\"\n*/\ntype conditional = ^{\n    if(1 extends string) {\n        return \"string\"\n    } else {\n        return \"number\"\n    }\n}\n```\n\nnested: \n\n```ts\n/*\n  type conditional2 = 1 extends string ? \"string\" : 1 extends 1 ? \"is 1\" : \"not 1\";\n*/\ntype conditional2 = ^{\n    if(1 extends string) {\n        return \"string\"\n    } else {\n        return ^{\n            if(1 extends 1) {\n                return \"is 1\"\n            } else {\n                return \"not 1\"\n            }\n        }\n    }\n}\n```\n\n### Mapped type\n\n```ts\n/* type mapped1 = { [K in Keys]: boolean } */\ntype mapped1 = ^{\n    for(K in Keys) {\n        return {\n            key: K,\n            value: boolean\n        }\n    }\n}\n```\n\n```ts\n/* type mapped2 = { [K in Keys as `get${K}`]: () =\u003e string } */\ntype mapped2 = ^{\n    for(K in Keys) {\n        return {\n            key: `get${K}`,\n            value: type () =\u003e string\n        }\n    }\n}\n```\n\n### Generic\n\n```ts\n/* export type Foo\u003cT\u003e = T extends { a: infer U; b: infer U; } ? U : never */\ntype function Foo = (T) =\u003e ^{\n    if(T extends {a: infer U, b: infer U}) {\n        return U\n    } else {\n        return never\n    }\n}\n```\n\nWith constraint:\n\n* [examples/type-challenges/4-easy-pick](https://github.com/mistlog/typetype-examples/blob/main/examples/type-challenges/4-easy-pick.type)\n\n```ts\n/* export type MyPick\u003cT, Keys extends keyof T\u003e = { [K in Keys]: T[K] } */\nexport type function MyPick = (T, Keys extends keyof T) =\u003e ^{\n    for(K in Keys) {\n        return {\n            key: K,\n            value: T[K]\n        }\n    }\n}\n```\n\n### Object spread\n\nObject spread syntax can be used, and it will be translated to `object$assign\u003c{}, [...]\u003e`:\n\n* [examples/url-parser-2/url-parser-2.type](https://github.com/mistlog/typetype-examples/blob/main/examples/url-parser-2/url-parser-2.type)\n\n```ts\nexport type function parseURL = (text) =\u003e ^{\n    if (parseProtocol\u003ctext\u003e extends [infer protocol, infer rest]) {\n        return {\n            protocol,\n            ...parseAuthority\u003crest\u003e\n        }\n    } else {\n        return never\n    }\n}\n```\n\nas long as `object$assign` is available globally, this works fine:\n\n```ts\nexport type parseURL\u003ctext\u003e = parseProtocol\u003ctext\u003e extends [infer protocol, infer rest] ? object$assign\u003c{}, [{\n  protocol: protocol;\n}, parseAuthority\u003crest\u003e]\u003e : never;\n```\n\nyou can polyfill it using type lib such as [ts-toolbelt](https://github.com/millsp/ts-toolbelt), for example: [polyfill/global.d.ts](https://github.com/mistlog/typetype-examples/blob/main/polyfill/global.d.ts).\n\n## How it works?\n\nIt's `AST -\u003e AST` transformation.\n\nWe use [react-peg](https://github.com/mistlog/react-peg) to write parser, as you can see in [./src/parser/expression](./src/parser/expression/expression.tsx), generator is even simpler than parser, in [./src/generator/generator](./src/generator/generator.ts), `typetype AST` is used to generate corresponding `babel AST`.\n\n## License\n\nThis project is [MIT licensed](https://github.com/mistlog/typetype/blob/master/LICENSE).\n","funding_links":[],"categories":["TypeScript","目录","Types"],"sub_categories":["TypeScript","IDE"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistlog%2Ftypetype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmistlog%2Ftypetype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistlog%2Ftypetype/lists"}