Comparison with vanilla typescript
Common operations ported to fp-ts
import { pipe } from "fp-ts/lib/pipeable";
import * as O from "fp-ts/lib/Option";
import * as A from "fp-ts/lib/Array";OR || variable assignment
|| variable assignmentconst a: string | undefined = undefined;
const b: string | undefined = "Present";
const result1 = a || b; // "Present"
const result2 = pipe(
O.fromNullable(a),
O.alt(() => O.fromNullable(b))
);
// O.some("Present")const a: string | undefined = undefined;
const d: string = "Default"; // always present
// keep the option
const result3 = pipe(
O.fromNullable(a),
O.alt(() => O.some(d))
);
// O.some("Default")
// exiting the option
const result4 = pipe(
O.fromNullable(a),
O.getOrElse(() => d)
);
// "Default"AND && Check for values
&& Check for valuesOptional chaining foo?.bar
foo?.barLast updated