{"id":13452528,"url":"https://github.com/gcanti/fp-ts-codegen","last_synced_at":"2025-07-03T22:36:55.642Z","repository":{"id":57241183,"uuid":"166963165","full_name":"gcanti/fp-ts-codegen","owner":"gcanti","description":"TypeScript code generation from a haskell-like syntax for ADT. Playground:","archived":false,"fork":false,"pushed_at":"2019-11-04T07:11:30.000Z","size":1244,"stargazers_count":106,"open_issues_count":1,"forks_count":9,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-16T05:51:14.377Z","etag":null,"topics":["algebraic-data-types","fp-ts","typescript"],"latest_commit_sha":null,"homepage":"https://gcanti.github.io/fp-ts-codegen/","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/gcanti.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-01-22T09:12:00.000Z","updated_at":"2025-03-12T23:12:58.000Z","dependencies_parsed_at":"2022-09-08T00:40:50.543Z","dependency_job_id":null,"html_url":"https://github.com/gcanti/fp-ts-codegen","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcanti%2Ffp-ts-codegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcanti%2Ffp-ts-codegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcanti%2Ffp-ts-codegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcanti%2Ffp-ts-codegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gcanti","download_url":"https://codeload.github.com/gcanti/fp-ts-codegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243936410,"owners_count":20371509,"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":["algebraic-data-types","fp-ts","typescript"],"created_at":"2024-07-31T07:01:26.640Z","updated_at":"2025-03-16T21:30:29.566Z","avatar_url":"https://github.com/gcanti.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)\n\n# Playground\n\n[A playground with a few examples](https://gcanti.github.io/fp-ts-codegen/)\n\n# Installation\n\nTo install the stable version:\n\n```sh\nnpm install fp-ts-codegen\n```\n\n# Usage\n\nSignature\n\n```ts\nfunction run(input: string, options: Options = defaultOptions): Either\u003cstring, string\u003e\n```\n\nExample\n\n```ts\nimport { run } from 'fp-ts-codegen'\n\nconsole.log(run('data Option A = None | Some A'))\n```\n\nOutput\n\n```ts\n/** type definition */\n\nexport type Option\u003cA\u003e =\n  | {\n      readonly type: 'None'\n    }\n  | {\n      readonly type: 'Some'\n      readonly value0: A\n    }\n\n/** constructors */\n\nexport const none: Option\u003cnever\u003e = { type: 'None' }\n\nexport function some\u003cA\u003e(value0: A): Option\u003cA\u003e {\n  return { type: 'Some', value0 }\n}\n\n/** pattern matching */\n\nexport function fold\u003cA, R\u003e(onNone: () =\u003e R, onSome: (value0: A) =\u003e R): (fa: Option\u003cA\u003e) =\u003e R {\n  return fa =\u003e {\n    switch (fa.type) {\n      case 'None':\n        return onNone()\n      case 'Some':\n        return onSome(fa.value0)\n    }\n  }\n}\n\n/** prisms */\n\nimport { Prism } from 'monocle-ts'\n\nexport function _none\u003cA\u003e(): Prism\u003cOption\u003cA\u003e, Option\u003cA\u003e\u003e {\n  return Prism.fromPredicate(s =\u003e s.type === 'None')\n}\n\nexport function _some\u003cA\u003e(): Prism\u003cOption\u003cA\u003e, Option\u003cA\u003e\u003e {\n  return Prism.fromPredicate(s =\u003e s.type === 'Some')\n}\n\n/** Eq instance */\n\nimport { Eq } from 'fp-ts/lib/Eq'\n\nexport function getEq\u003cA\u003e(eqSomeValue0: Eq\u003cA\u003e): Eq\u003cOption\u003cA\u003e\u003e {\n  return {\n    equals: (x, y) =\u003e {\n      if (x === y) {\n        return true\n      }\n      if (x.type === 'None' \u0026\u0026 y.type === 'None') {\n        return true\n      }\n      if (x.type === 'Some' \u0026\u0026 y.type === 'Some') {\n        return eqSomeValue0.equals(x.value0, y.value0)\n      }\n      return false\n    }\n  }\n}\n```\n\n# Records\n\nSyntax: `{ name :: type }`\n\nExample\n\nSource\n\n```haskell\n--     record ---v\ndata User = User { name :: string, surname :: string }\n```\n\nOutput\n\n```ts\nexport type User = {\n  readonly type: 'User'\n  readonly name: string\n  readonly surname: string\n}\n\nexport function user(name: string, surname: string): User {\n  return { type: 'User', name, surname }\n}\n```\n\n# Tuples\n\nSyntax: `(type1, type2, ...types)`\n\nExample\n\nSource\n\n```haskell\n--              tuple ---v\ndata Tuple2 A B = Tuple2 (A, B)\n```\n\nOutput\n\n```ts\nexport type Tuple2\u003cA, B\u003e = {\n  readonly type: 'Tuple2'\n  readonly value0: [A, B]\n}\n\nexport function tuple2\u003cA, B\u003e(value0: [A, B]): Tuple2\u003cA, B\u003e {\n  return { type: 'Tuple2', value0 }\n}\n```\n\n# Constraints\n\nSyntax: `(\u003cname\u003e :: \u003cconstraint\u003e)`\n\nExample\n\nSource\n\n```haskell\n--    constraint ---v\ndata Constrained (A :: string) = Fetching | GotData A\n```\n\nOutput\n\n```ts\nexport type Constrained\u003cA extends string\u003e =\n  | {\n      readonly type: 'Fetching'\n    }\n  | {\n      readonly type: 'GotData'\n      readonly value0: A\n    }\n```\n\n# Options\n\n```ts\n// fp-ts-codegen/lib/ast module\n\nexport interface Options {\n  /** the name of the field used as tag */\n  tagName: string\n  /** the name prefix used for pattern matching functions */\n  foldName: string\n  /**\n   * the pattern matching handlers can be expressed as positional arguments\n   * or a single object literal `tag -\u003e handler`\n   */\n  handlersStyle: { type: 'positional' } | { type: 'record'; handlersName: string }\n}\n\nexport const defaultOptions: Options = {\n  tagName: '_tag',\n  foldName: 'fold',\n  handlersStyle: { type: 'positional' }\n}\n```\n\n## Options management\n\nExample\n\n```ts\nimport { lenses, defaultOptions } from 'fp-ts-codegen/lib/ast'\n\nlenses.tagName.set('tag')(defaultOptions)\n/*\n{\n  tagName: 'tag',\n  foldName: 'fold',\n  ...\n}\n*/\n```\n\n# Modules\n\n- `ast` module: internal model -\u003e TypeScript AST\n- `model` module: internal model\n- `printer` module: internal model -\u003e TypeScript code\n- `haskell` module: haskell-like syntax -\u003e internal model\n- `index` module: haskell-like syntax -\u003e TypeScript code\n\n# Roadmap\n\n- derive type class instances? (`Functor`, `Foldable`, etc...)\n- ???\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgcanti%2Ffp-ts-codegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgcanti%2Ffp-ts-codegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgcanti%2Ffp-ts-codegen/lists"}