{"id":13726976,"url":"https://github.com/hasparus/nom-ts","last_synced_at":"2025-08-10T22:48:47.624Z","repository":{"id":65531277,"uuid":"197823018","full_name":"hasparus/nom-ts","owner":"hasparus","description":"🤓 TypeScript nominal-typing helpers","archived":false,"fork":false,"pushed_at":"2023-01-07T14:51:34.000Z","size":250,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-04T09:06:58.060Z","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/hasparus.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-07-19T18:22:51.000Z","updated_at":"2023-06-14T00:13:50.000Z","dependencies_parsed_at":"2023-02-07T11:31:43.099Z","dependency_job_id":null,"html_url":"https://github.com/hasparus/nom-ts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hasparus/nom-ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasparus%2Fnom-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasparus%2Fnom-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasparus%2Fnom-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasparus%2Fnom-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hasparus","download_url":"https://codeload.github.com/hasparus/nom-ts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasparus%2Fnom-ts/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269803693,"owners_count":24477649,"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":[],"created_at":"2024-08-03T01:03:33.989Z","updated_at":"2025-08-10T22:48:47.578Z","avatar_url":"https://github.com/hasparus.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# nom-ts\n\nTypeScript helpers for nominal typing\n\n---\n\n# Problem\n\n- 😀 Nominal types are [pretty useful](https://github.com/Microsoft/TypeScript/blob/69abe49930761aea92dc564f9b6a5db74d6e1be9/src/compiler/types.ts#L1183-L1192).\n- 😞 TypeScript doesn't support nominal types ([yet?](https://github.com/microsoft/TypeScript/wiki/Roadmap/69ede9ef95de7bf8764a2f2fa3c3d4c441cc828d#future)).\n\nGreat! This is easy to fix right?\nThe simple trick below solves the problem.\n\n```ts\ndeclare const __brand: unique symbol;\n\ndeclare const __type: unique symbol;\n\ninterface __Brand\u003cT, S extends string\u003e {\n  [__brand]: S;\n  [__type]: T;\n}\n\nexport type Brand\u003cT, S extends string\u003e = T \u0026 __Brand\u003cT, S\u003e;\n```\n\nBut then we encounter some more happy little problems.\n\n```ts\ntype PlayerId = Brand\u003cnumber, 'PlayerId'\u003e;\ntype PlayerScores = Record\u003cPlayerId, number\u003e;\n\nconst myId = 1337 as PlayerId;\nconst scores: PlayerScores = { [myId]: 9001 };\n\n// Element implicitly has an 'any' type because expression\n// of type 'Brand\u003cnumber, \"PlayerId\"\u003e' can't be used to\n// index type 'Record\u003cBrand\u003cnumber, \"PlayerId\"\u003e, number\u003e'.\n// ts(7053)\nconst myScore = scores[myId];\n```\n\n**nom-ts aims to address these problems.**\n\n# Goals\n\n- 🦅 no dependencies\n- 🦥 all branded types in nom-ts are assignable to their underlying type\n  without any additional syntax\n\n# The rest of the code\n\n```ts\ninterface __Flavor\u003cT, S extends string\u003e {\n  [__brand]?: S;\n  /**\n   * Intersection is distributive over union, but we don't want to \"lose\" information about T.\n   * `__type?: T` will be useful for Unbrand.\n   */\n  [__type]?: T;\n}\n\nexport type Flavor\u003cT, S extends string\u003e = T \u0026 __Flavor\u003cT, S\u003e;\n\nexport type Unbrand\u003cT\u003e = T extends Flavor\u003cinfer X, any\u003e ? X : T;\n\nexport const Unbrand = \u003cT extends any\u003e(x: T) =\u003e x as Unbrand\u003cT\u003e;\n\nexport type Dict\u003cK extends PropertyKey, V\u003e = Record\u003cUnbrand\u003cK\u003e, V\u003e;\n```\n\n# Links\n\n- https://github.com/Microsoft/TypeScript/issues/202\n- https://basarat.gitbooks.io/typescript/docs/tips/nominalTyping.html\n- \"Brand\" name is taken from\n  https://michalzalecki.com/nominal-typing-in-typescript/\n- \"Flavor\" name is taken from\n  https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/\n- https://github.com/gcanti/newtype-ts\n\n----\n\n#### Credits\n\nThis project was bootstrapped with [TSDX](https://github.com/jaredpalmer/tsdx).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasparus%2Fnom-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhasparus%2Fnom-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasparus%2Fnom-ts/lists"}