{"id":22093192,"url":"https://github.com/iadvize/opaque-type-library","last_synced_at":"2025-07-24T20:32:39.281Z","repository":{"id":42934129,"uuid":"237646617","full_name":"iadvize/opaque-type-library","owner":"iadvize","description":"@iadvize-oss/opaque-type - opaque type for Typescript","archived":false,"fork":false,"pushed_at":"2023-04-25T13:21:37.000Z","size":2178,"stargazers_count":2,"open_issues_count":29,"forks_count":2,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-10-29T00:59:08.380Z","etag":null,"topics":["typescript"],"latest_commit_sha":null,"homepage":"","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/iadvize.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-01T16:55:54.000Z","updated_at":"2023-04-25T13:21:42.000Z","dependencies_parsed_at":"2024-06-19T01:55:10.167Z","dependency_job_id":null,"html_url":"https://github.com/iadvize/opaque-type-library","commit_stats":{"total_commits":388,"total_committers":4,"mean_commits":97.0,"dds":"0.17268041237113407","last_synced_commit":"cfa4f673e39323d6d21395e3f600a97a6b1ea9c9"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":"iadvize/hello-world-javascript-library","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fopaque-type-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fopaque-type-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fopaque-type-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fopaque-type-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iadvize","download_url":"https://codeload.github.com/iadvize/opaque-type-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227040946,"owners_count":17722019,"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":["typescript"],"created_at":"2024-12-01T03:13:15.814Z","updated_at":"2024-12-01T03:13:16.414Z","avatar_url":"https://github.com/iadvize.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"@iadvize/opaque-types\n====================\n![Continuous integration](https://github.com/iadvize/opaque-type-library/workflows/Continuous%20integration/badge.svg)\n\n\nInspired from https://codemix.com/opaque-types-in-javascript/\n\n# Usage\n\n```\nnpm add @iadvize-oss/opaque-type\n```\n\n[📖 Documentation](https://iadvize.github.io/opaque-type-library/)\n\n# Why ? \n\n## Semantic type checking\n\nThe following naive code will compile properly, except that if you mix\n`createRoom` parameters you will introduce a nasty bug at runtime.\n\n```typescript\ntype RoomID = string;\ntype UserID = string;\n\nfunction createRoom(roomId: RoomID, userId: UserID) {\n  // something\n}\n\nconst roomId: RoomID = ...;\nconst userId: UserID = ...;\ncreateRoom(userId, roomId);\n```\n\nOpaque at rescue !\n\nThis following code won't compile, because `Opaque` add more specificty to\n`RoomId` and `UserId`, so typescript wont think they are compatible because they\nare built uppon a `string`;\n\n```typescript\nimport { Opaque } from \"@iadvize-oss/opaque-type\";\n\ntype RoomID = Opaque\u003c\"RoomId\"\u003e;\ntype UserID = Opaque\u003c\"UserId\"\u003e;\n\nfunction createRoom(roomId: RoomID, userId: UserID) {\n  // something\n}\n\nconst roomId: RoomID = ...;\nconst userId: UserID = ...;\n\ncreateRoom(userId, roomId); // TypeError\n```\n\nHere the error message you will get in your editor or when you will try to build\nyour application\n\n```\nArgument of type 'Opaque\u003c\"UserId\"\u003e' is not assignable to parameter of type 'Opaque\u003c\"RoomId\"\u003e'.\n  ...\n```\n\n\n## Type opacification (hence the name)\n\nLet say you built a nice piece of code:\n\n```typescript\n// message.ts\ntype Message = {\n  userId: string,\n  text: string,\n};\n\nexport function clearMessageText(message: Message) : Message {\n  return message.text = '';\n}\n\nexport function getMessage():Message {\n  return ...\n}\n```\n\nYou user can endup writting the following code:\n\n```typescript\nimport { clearMessageText, getMessage } from './message';\n\nconst message = { userId: 'userId', text: 'mytext' };\n\nclearMessageText(message); \n\n```\n\nor\n\n```typescript\nimport { getMessage } from '..'\n\nconst message2 = getMessage();\n\nmessage2.userId = 'whatever';\n```\n\nThose two pieces of code are problematic, because in these the inner structure\nof your types are exposed and manipulated.\n\nIf you make any change to the `Message` structure it will be a breaking change\nfor your code user. Meaning that he could find himself to edit dozen or hundreds\nof line of code.\n\nHow can we avoid this protecting our user from creating a code strongly tied to\nyour library internals ?\n\n```typescript\n// message.ts\nimport { createOpaqueAPI } from '../index';\n\ntype $Message = {\n  userId: string,\n  text: string,\n};\n\nconst { toOpaque, fromOpaque } = createOpaqueAPI\u003c\n  'Message',\n  $Message,\n\u003e('Message');\n\ntype Message = ReturnType\u003ctypeof toOpaque\u003e;\n\nexport function getMessage(): Message {\n  return toOpaque({ userId: 'userId', text: 'text' });\n}\n\nexport function clearMessageText(message: Message): Message {\n  const $message = fromOpaque(message)\n\n  return toOpaque({\n    ...$message,\n    text: '',\n  });\n}\n```\n\nYour user won't be able to write code in same fashion, he will have to ignore\nthe internals of your code.\n\n```typescript\nimport { getMessage, clearMessageText } from '../index';\n\nconst message = getMessage();\n\nconst messageCleared = clearMessageText(message); \n```\n\nThis is especially usefull when creating a reusable a library:\n\n- It does protect the user of your code from himself\n- It does protect you the maintainer by allowing you to break type structure\n  behind the scène without creating a breaking change.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Fopaque-type-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiadvize%2Fopaque-type-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Fopaque-type-library/lists"}