{"id":21556876,"url":"https://github.com/falinor/fp-ts-cheatsheet","last_synced_at":"2026-01-04T03:44:08.633Z","repository":{"id":45857034,"uuid":"429455319","full_name":"Falinor/fp-ts-cheatsheet","owner":"Falinor","description":null,"archived":false,"fork":false,"pushed_at":"2021-12-01T15:15:54.000Z","size":59,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T10:31:59.692Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/Falinor.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}},"created_at":"2021-11-18T14:12:10.000Z","updated_at":"2021-12-01T15:15:57.000Z","dependencies_parsed_at":"2022-09-15T18:51:32.053Z","dependency_job_id":null,"html_url":"https://github.com/Falinor/fp-ts-cheatsheet","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/Falinor%2Ffp-ts-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Falinor%2Ffp-ts-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Falinor%2Ffp-ts-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Falinor%2Ffp-ts-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Falinor","download_url":"https://codeload.github.com/Falinor/fp-ts-cheatsheet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244147614,"owners_count":20406013,"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-11-24T08:10:09.042Z","updated_at":"2026-01-04T03:44:08.607Z","avatar_url":"https://github.com/Falinor.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# fp-ts cheatsheet\n\nThis cheatsheet is intended for any reader to understand and remember fp-ts' most used features.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**\n\n- [Combining functions](#combining-functions)\n  - [Pipe](#pipe)\n  - [Flow](#flow)\n- [Common constructors](#common-constructors)\n  - [From a nullable value](#from-a-nullable-value)\n  - [Using a predicate](#using-a-predicate)\n  - [Using a refinement](#using-a-refinement)\n- [Either](#either)\n  - [Useful functions](#useful-functions)\n- [Task](#task)\n  - [Useful functions](#useful-functions-1)\n- [TaskEither](#taskeither)\n  - [Useful functions](#useful-functions-2)\n- [Bind](#bind)\n- [What does `W` in `chainW` mean?](#what-does-w-in-chainw-mean)\n- [What does `K` in `fromNullableK` mean?](#what-does-k-in-fromnullablek-mean)\n- [Glossary](#glossary)\n  - [Context value](#context-value)\n  - [Predicate](#predicate)\n  - [Refinement](#refinement)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Combining functions\n\n### Pipe\nDefinition: transform an expression in a data-first pipeline of functions.\n```ts\nimport { pipe } from 'fp-ts/function'\n\nconst masters = ['Yoda', 'Vador', 'Luke']\npipe(\n  masters,\n  map(master =\u003e master.length),\n  filter(length =\u003e length \u003e 4)\n)\n```\n\n### Flow\nDefinition: like pipe but data-last.\n```ts\nimport { flow } from 'fp-ts/function'\n\nconst value = ['Yoda', 'Vador', 'Luke']\nflow(\n  map(master =\u003e master.length),\n  filter(length =\u003e length \u003e 4),\n)(masters)\n```\n\n## Common constructors\n\nFor all the monads that can fail below, these constructors exists\n\n### From a nullable value\n```ts\nimport { fromNullable } from 'fp-ts/Either'\n\n// Without type signature\nconst mustExist = fromNullable(new CartMissingError())\n\n// With explicit type signature\nconst mustExist: (cart: Cart | null) =\u003e Either\u003cCartMissingError, Cart\u003e = fromNullable(new CartMissingError())\n```\n\n### Using a [predicate](#predicate)\nTest whether a predicate is true and return an error if not.\n```ts\nimport { fromPredicate } from 'fp-ts/Either'\n\nimport { PropDifferentError } from '../errors'\n\nconst mustBeEqual: (obj: MyObject) =\u003e Either\u003cPropDifferentError, MyObject\u003e = fromPredicate(\n  obj =\u003e obj.prop === 'value',\n  obj =\u003e new PropDifferentError(obj.prop, 'value')\n)\n```\n\n### Using a [refinement](#refinement)\nLike a predicate but casts the given value into another type.\n```ts\nimport { fromPredicate } from 'fp-ts/Either'\n\nimport { NotCartError } from '../errors'\n\nconst mustBeCart: (cartOrList: Cart | List) =\u003e Either\u003cNotCartError, cartOrList is Cart\u003e = fromPredicate(\n  cartOrList =\u003e cartOrList.type === 'cart',\n  cartOrList =\u003e new NotCartError()\n)\n```\n\n## Either\nDefinition: an error (left) or a value (right).\n```ts\ninterface Left\u003cE\u003e {\n  readonly _tag: 'Left'\n  readonly left: E\n}\n\ninterface Right\u003cA\u003e {\n  readonly _tag: 'Right'\n  readonly right: A\n}\n\ntype Either\u003cE, A\u003e = Left\u003cE\u003e | Right\u003cA\u003e\n```\n\n### Useful functions\nReturn error.\n```ts\nfunction left\u003cE = never, A = never\u003e(e: E) =\u003e Either\u003cE, A\u003e\n\nleft(new Error('error'))\n```\n\nReturn success.\n```ts\nfunction right\u003cE = never, A = never\u003e(a: A) =\u003e Either\u003cE, A\u003e\n\nright(42)\n```\n\nMap the inner type to another type.\n```ts\nfunction map\u003cA, B\u003e(f: (a: A) =\u003e B): \u003cE\u003e(fa: Either\u003cE, A\u003e) =\u003e Either\u003cE, B\u003e\n\nconst either = right('string')\nmap(str =\u003e str.length)(either)\n```\n\nChain with another Either.\n```ts\nfunction chain\u003cE, A, B\u003e(f: (a: A) =\u003e Either\u003cE, B\u003e): (ma: Either\u003cE, A\u003e) =\u003e Either\u003cE, B\u003e\n\npipe(\n  right(age),\n  chain(age =\u003e age \u003c 18 ? left(new TooYoungError(age)) : right(age)\n)\n\n// With `fromPredicate`\nconst isOldEnough = fromPredicate(\n  age =\u003e age \u003c 18,\n  age =\u003e new TooYoungError(age)\n)\n\npipe(\n  right(age),\n  chain(isOldEnough)\n)\n```\nYou may use `Either.chainW` to [_widen_ the error type](#what-does-w-in-chainw-mean).\n\n## Task\nDefinition: an asynchronous computation that yields a value and **never fails**.\n```ts\ninterface Task\u003cA\u003e {\n  (): Promise\u003cA\u003e\n}\n```\n\nTask is often used when one destroys a TaskEither e.g.\n```ts\nawait pipe(\n  useCase.execute(), // =\u003e TaskEither\u003cE, A\u003e\n  fold(\n    error =\u003e Task.of(error),\n    value =\u003e Task.of(value)\n  )\n)()\n```\n### Useful functions\n\ndelay(millis: number) : Creates a task that will complete after a time delay.\n```ts\n  pipe(\n      T.chain(() =\u003e doSomething()),\n      T.chain(() =\u003e delay(1000)),\n      T.map(() =\u003e 'done after 1000ms')\n  )\n```\n\n## TaskEither\nDefinition: an asynchronous computation that **either yields a value** of type A or **fails, yielding an error** of type E\n```ts\ninterface TaskEither\u003cE, A\u003e extends Task\u003cEither\u003cE, A\u003e\u003e {}\n```\n\n### Useful functions\n\nTODO\n\n## Bind\nDefinition: bind allows to store a read-only context value in the composition pipeline, so that it can be reused later.\n\n```ts\npipe(\n  bind('user', fromThunk(userRepository.save)),\n  // Here is the ContextValue\n  ({ user }) =\u003e {\n    console.log(user)\n  }\n)\n\n// This is inferred by typescript\ninterface ContextValue {\n  readonly user: User\n}\n```\nIt forbids you to mutate the context value.\n\n## What does `W` in `chainW` mean?\n`W` means a function is able to aggregate errors into a union (for Either based data types) or environments into an intersection (for Reader based data types).\n```ts\nimport * as E from 'fp-ts/Either'\nimport * as TE from 'fp-ts/TaskEither'\nimport { pipe } from 'fp-ts/function'\n\ndeclare function parseString(s: string): E.Either\u003cstring, number\u003e\ndeclare function fetchUser(id: number): TE.TaskEither\u003cError, User\u003e\n\n// This raises an error because: Type 'string' is not assignable to type 'Error'\nconst program_ = (s: string) =\u003e pipe(s, TE.fromEitherK(parseString), TE.chain(fetchUser))\n\n// const program: (s: string) =\u003e TE.TaskEither\u003cstring | Error, User\u003e\nconst program = (s: string) =\u003e pipe(s, TE.fromEitherK(parseString), TE.chainW(fetchUser))\n```\n\n## What does `K` in `fromNullableK` mean?\n`K` in `fromNullableK` is used to do a `fromNullable` with a transformation (by a function) apply to the given agurments `\u003cA\u003e`\nAnd then return either `NonNullable\u003cB\u003e` or an error\n\n```ts\nexport declare const fromNullableK: \u003cE\u003e(\n  e: E\n) =\u003e \u003cA extends readonly unknown[], B\u003e(f: (...a: A) =\u003e B | null | undefined) =\u003e (...a: A) =\u003e Either\u003cE, NonNullable\u003cB\u003e\u003e\n```\n\n\n`K` means Kleisli. A Kleisli arrow is a function with the following signature\n```ts\n(a: A) =\u003e F\u003cB\u003e\n```\nIt's a sufix that we can find on other functions around the lib. ([more info on sufix](https://gcanti.github.io/fp-ts/guides/code-conventions.html))\n\n\n## Glossary\n\n### Context value\nThe value that transits throughout a pipe. See [bind](#bind).\n\n### Predicate\nA predicate is a function with the following signature:\n```ts\n\u003cA\u003e(a: A) =\u003e boolean\n```\n\n### Refinement\nA refinement is a special type of predicate that casts the given value in another type.\n```ts\n\u003cA, B\u003e(a: A) =\u003e a is B\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalinor%2Ffp-ts-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffalinor%2Ffp-ts-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalinor%2Ffp-ts-cheatsheet/lists"}