different fp-ts utils
This package includes eslint-plugin-fpts-style, a custom ESLint plugin that enforces fp-ts functional programming best practices. The plugin provides 18 rules to ensure consistent, maintainable fp-ts code:
Core Style Rules:
no-statements-outside-pipe- Functions must return a single pipe expressionprefer-flatmap-over-chain- UseflatMapinstead of deprecatedchain/chainWno-nested-pipes- Prevents deeply nested pipes (configurable threshold)no-long-inline-functions-in-pipe- Extract long inline functions (max 5 lines by default)prefer-concise-arrow-function- Encourages concise arrow function syntaxprefer-merged-short-pipes- Suggests merging short consecutive pipes
Code Quality:
no-const-variables- Preferletoverconst(except UPPER_CASE constants)no-async-await- Use TaskEither instead of async/awaitenforce-file-layout- Exports first, then private helpersno-pipe-in-brackets- Prevents pipes wrapped in unnecessary bracketsno-unnecessary-currying- Simplifies overly-curried functionsprefer-grouped-parameters- Groups related parameters for readability
fp-ts Specific:
no-fp-ts-lib-imports- Use ti-fptsu aliases instead of direct fp-ts importsprefer-a-map- Usea.mapinstead of native array.map()in fp-ts codesimplify-task-constructors- Simplifies Task/TaskEither constructor patternsprefer-flow-over-pipe- Useflowfor function compositionrequire-flatmap-for-task-returns- Ensures proper flatMap when returning Tasksno-unnecessary-thunk-in-io-of- Removes unnecessary thunks in IO.of
Many rules are auto-fixable with --fix. See eslint-plugin-fpts-style/README.md for detailed documentation and examples.
yarn add fp-ts ti-fptsuThe eslint-plugin-fpts-style plugin is bundled with ti-fptsu. To use it:
- Install dependencies:
pnpm add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser ti-fptsu- Create
eslint.config.js:
import tsParser from "@typescript-eslint/parser"
import fptsStyle from "ti-fptsu/eslint-plugin-fpts-style"
export default [
{
files: ["**/*.ts", "**/*.js"],
languageOptions: {
parser: tsParser,
ecmaVersion: 2020,
sourceType: "module",
},
plugins: {
"fpts-style": fptsStyle,
},
rules: {
...fptsStyle.configs.recommended.rules,
},
},
{
files: ["**/*.test.ts"],
rules: {
"fpts-style/no-async-await": "off",
"fpts-style/no-unnecessary-currying": "off",
"fpts-style/prefer-grouped-parameters": "off",
},
},
{
ignores: ["**/node_modules/**", "**/dist/**", "**/build/**"],
},
]- Add lint script to
package.json:
{
"scripts": {
"lint": "eslint ."
}
}Instead of importing from fp-ts directly, use the shorter aliases:
import { te, o, e, a, pipe } from "ti-fptsu/lib"
// Instead of:
// import * as TE from "fp-ts/lib/TaskEither.js"
// import * as O from "fp-ts/lib/Option.js"
// import * as E from "fp-ts/lib/Either.js"
// import * as A from "fp-ts/lib/Array.js"
// import { pipe } from "fp-ts/lib/function.js"
let result = pipe(
te.of(42),
te.map((x) => x * 2),
te.flatMap((x) => te.of(x + 1)),
)Available aliases:
te- TaskEithert- Taskrte- ReaderTaskEithero- Optione- Eithera- Arraynea- NonEmptyArrayb- booleanio- IOpipe- function pipeflow- function flow
Type aliases:
TaskE<E, A>- TaskEither<E, A>ReaderTE<R, E, A>- ReaderTaskEither<R, E, A>Opt<A>- Option