{"id":22268724,"url":"https://github.com/paveldymkov/ts-type-from-enum","last_synced_at":"2025-03-25T15:20:06.985Z","repository":{"id":40722852,"uuid":"506711493","full_name":"PavelDymkov/ts-type-from-enum","owner":"PavelDymkov","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-27T16:27:29.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T13:11:20.517Z","etag":null,"topics":[],"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/PavelDymkov.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":"2022-06-23T16:21:38.000Z","updated_at":"2022-06-27T15:49:41.000Z","dependencies_parsed_at":"2022-07-29T06:18:08.672Z","dependency_job_id":null,"html_url":"https://github.com/PavelDymkov/ts-type-from-enum","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/PavelDymkov%2Fts-type-from-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PavelDymkov%2Fts-type-from-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PavelDymkov%2Fts-type-from-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PavelDymkov%2Fts-type-from-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PavelDymkov","download_url":"https://codeload.github.com/PavelDymkov/ts-type-from-enum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245486358,"owners_count":20623244,"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":[],"created_at":"2024-12-03T11:13:39.473Z","updated_at":"2025-03-25T15:20:06.964Z","avatar_url":"https://github.com/PavelDymkov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-type-from-enum\n\n![test: passed](https://raw.githubusercontent.com/PavelDymkov/ts-type-from-enum/main/badges/test.svg)\n\n## Usage\n\n```ts\nimport { EnumMap, UnionFrom } from \"ts-type-from-enum\";\n\nenum ShapeKind {\n    Rect,\n    Circle,\n}\n\ninterface ShapeBaseType {\n    kind: ShapeKind;\n}\n\ninterface Rect extends ShapeBaseType {\n    kind: ShapeKind.Rect;\n    width: number;\n    height: number;\n}\n\ninterface Circle extends ShapeBaseType {\n    kind: ShapeKind.Circle;\n    radius: number;\n}\n\ntype Shape = UnionFrom\u003c\n    EnumMap\u003c\n        ShapeKind,\n        {\n            [ShapeKind.Rect]: Rect;\n            [ShapeKind.Circle]: Circle;\n            // if add a new kind to ShapeKind but\n            // forget to add an interface here, an error will be emitted\n        },\n        ShapeBaseType\n    \u003e\n\u003e;\n\n// The same as\ntype Shape = Rect | Circle;\n\nconst shape: Shape = {\n    kind: ShapeKind.Circle,\n    radius: 1,\n};\n\nif (shape.kind === ShapeKind.Circle) {\n    console.log(shape.radius); // ok\n    console.log(shape.width); // error\n}\n```\n\n```ts\nimport { EnumMap, EnumTemplate, UnionFrom } from \"ts-type-from-enum\";\n\nenum Input {\n    Init = \"init\",\n    Change = \"change\",\n    Destroy = \"destroy\",\n}\n\ntype Params = EnumMap\u003c\n    Input,\n    {\n        [Input.Init]: [boolean];\n        [Input.Change]: [string];\n        [Input.Destroy]: [number, string];\n    }\n\u003e;\n\ntype Variants = UnionFrom\u003c\n    EnumTemplate\u003c\n        Input,\n        {\n            type: Input;\n            params: Params;\n            foo: string;\n        }\n    \u003e\n\u003e;\n\n// The same as\ntype Variants =\n    | {\n          type: Input.Init;\n          params: [boolean];\n          foo: string;\n      }\n    | {\n          type: Input.Change;\n          params: [string];\n          foo: string;\n      }\n    | {\n          type: Input.Destroy;\n          params: [number, string];\n          foo: string;\n      };\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldymkov%2Fts-type-from-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaveldymkov%2Fts-type-from-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldymkov%2Fts-type-from-enum/lists"}