Code conventions

fp-ts uses an approach ef exporting every single function from the module, to avoid name collision is better to import all the module and use an alias

For example this is an error

import { fold } from "fp-ts/lib/Either";
import { fold } from "fp-ts/lib/TaskEither";

it's possible to rename it but this practice will become old pretty quick, so we import everything with an alias

import * as E from "fp-ts/lib/Either";
import * as TE from "fp-ts/lib/TaskEither";
import * as T from "fp-ts/lib/Task";
import * as O from "fp-ts/lib/Option";

Why using only a couple of letter? because it becomes easier to read after a while.

for example let's use the full name for the alias

import * as Either from "fp-ts/lib/Either";
import * as TaskEither from "fp-ts/lib/TaskEither";
import * as Task from "fp-ts/lib/Task";
import * as Option from "fp-ts/lib/Option";

and use it in a simple function declaration

declare function f(e: Either.Either<string, string>): Option.Option<string>;

it's noisy, and the problem is even greater when pipe is introduces

let's try the short version for both this examples

pipe is the only exception because we only need that function from the module.

Here are a list of short names for the import that are used in this book

of course you can use what you find best, feel free to experiment.

Last updated

Was this helpful?