{"id":19016285,"url":"https://github.com/akhansari/ts-adt","last_synced_at":"2026-03-15T19:40:47.618Z","repository":{"id":227125885,"uuid":"770519864","full_name":"akhansari/ts-adt","owner":"akhansari","description":"Algebraic Data Types \u0026 Typescript","archived":false,"fork":false,"pushed_at":"2024-09-10T16:12:43.000Z","size":49,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-26T08:35:52.531Z","etag":null,"topics":["adt","algebraic-data-types","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akhansari.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":"2024-03-11T17:34:22.000Z","updated_at":"2024-09-10T16:12:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"a7b24e04-3d45-43ec-b318-265c5bad853d","html_url":"https://github.com/akhansari/ts-adt","commit_stats":null,"previous_names":["akhansari/ts-adt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/akhansari/ts-adt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akhansari%2Fts-adt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akhansari%2Fts-adt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akhansari%2Fts-adt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akhansari%2Fts-adt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akhansari","download_url":"https://codeload.github.com/akhansari/ts-adt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akhansari%2Fts-adt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30550539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-15T15:03:43.933Z","status":"ssl_error","status_checked_at":"2026-03-15T15:03:37.630Z","response_time":61,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["adt","algebraic-data-types","typescript"],"created_at":"2024-11-08T19:42:57.650Z","updated_at":"2026-03-15T19:40:47.594Z","avatar_url":"https://github.com/akhansari.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Algebraic Data Types \u0026 Typescript\n\nAlgebraic data types are a way of representing data by **combining** simple types using algebraic operations.\nThere are two main kinds of algebraic types: product types and sum types. Discriminated unions are a specific implementation of sum types.\n\n## Product types (AND types)\n\nProduct types represent a combination of types.\n\n```typescript\ntype Shirt = {\n    size: string\n    sleeveLength: string\n    shape: string\n}\n```\n\n## Sum types (OR types)\n\nSum types represent a choice between types.\n\n```typescript\ntype ShirtSize = \"S\" | \"M\" | \"L\" | \"XL\" | \"XXL\"\ntype SleeveLength = \"Short\" | \"Long\"\n```\n\n## Compose\n\nCompose small types from other small types.\n\n```typescript\ntype SleeveLength = \"Short\" | \"Long\"\n\ntype Shirt = {\n    size: \"S\" | \"M\" | \"L\" | \"XL\" | \"XXL\"\n    sleeveLength: SleeveLength\n    shape: string\n}\n```\n\n## Pattern Matching\n\nIt's possible by adding a \"kind\" field to the product type.\n\n```typescript\ntype SleeveLength = \"Short\" | \"Long\"\n\ntype Shirt = {\n    kind: \"Shirt\"\n    size: \"S\" | \"M\" | \"L\" | \"XL\" | \"XXL\"\n    sleeveLength: SleeveLength\n    shape: string\n}\n\ntype Jean = {\n    kind: \"Jean\"\n    hipSize: number\n    legSize: number\n    ...\n}\n\ntype Clothing = Shirt | Jean\n\nconst clothing: Clothing = {\n    kind: \"Shirt\",\n    size: \"L\",\n    sleeveLength: \"Short\",\n    shape: \"Straight\",\n}\n\nconst describeClothing = (clothing: Clothing) =\u003e {\n    switch (clothing.kind) {\n        case \"Shirt\": return \"This is a shirt!\"\n        case \"Jean\": return \"This is a jean!\"\n    }\n}\n```\n\nYou can go further by adding sections to kinds in order to combine Clothing and Shoes.\n\n- `kind: \"Clothing/Shirt\"`\n- `kind: \"Clothing/Jean\"`\n- `kind: \"Shoes/Sneakers\"`\n- `kind: \"Shoes/Boots\"`\n\n```typescript\ntype Product = {\n    id: number\n    price: Price\n    item: Clothing | Shoes\n}\n```\n\n## OpenAPI\n\nIn OpenAPI specification, sum types are represented by `oneOf`.\n\n```yaml\nClothing:\n  oneOf:\n    - $ref: \"#/components/schemas/Shirt\"\n    - $ref: \"#/components/schemas/Jean\"\n  discriminator:\n    propertyName: kind\n    mapping:\n      Shirt: \"#/components/schemas/Shirt\"\n      Jean: \"#/components/schemas/Jean\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakhansari%2Fts-adt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakhansari%2Fts-adt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakhansari%2Fts-adt/lists"}