Skip to content

Commit 8c4f933

Browse files
authored
Add an upgrade guide for v2 (#905)
* Add an upgrade guide for v2 * Add a note about the removal of non-lazy fallback functions * Adjust message about upgrading dependencies (With the class-less approach, tsc should be less prone to break.) * Recommend using `pipe` also for simple cases
1 parent 3831e49 commit 8c4f933

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

docs/introduction/upgrade-to-v2.md

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
title: Upgrade to fp-ts 2.x
3+
parent: Introduction
4+
nav_order: 5
5+
has_toc: false
6+
---
7+
8+
# Upgrade to version 2.x
9+
10+
`fp-ts@2.x` brings with it some major improvements, but also breaking changes and the removal of deprecated APIs. This document will help you understand what changed and how you can upgrade your existing codebase.
11+
{: .fs-6 .fw-300 }
12+
13+
---
14+
15+
The major changes in `fp-ts@2.x` are:
16+
17+
- Requires TypeScript 3.5+
18+
- `fp-ts@1.19.x` has been released with backported 2.x features for a gradual upgrade path
19+
- Data types are no longer implemented as classes, resulting in a new API using `pipe`
20+
- The `run()` method on `IO`, `Task`, etc. has been replaced with a thunk
21+
- Functions accepting fallback values are now always lazy (e.g. `getOrElseL` is now just `getOrElse`)
22+
- Deprecations
23+
- `HKT`: Replaced `Type<n>` with `Kind<n>`
24+
- Replaced `Setoid` with `Eq`
25+
- Several modules were removed, e.g. `Exception`, `Free`, `StrMap`, `Trace`, `Validation`, …
26+
- Read the [full changelog](https://github.com/gcanti/fp-ts/pull/881) for all the changes
27+
28+
## Upgrading from version 1.x
29+
30+
You can gradually upgrade your existing codebase using the `fp-ts@1.19.x` release; the new `fp-ts@2.x` APIs have been backported to this release.
31+
32+
1. Upgrade TypeScript to version 3.5+
33+
1. Install `fp-ts@1.19.x`, which contains the new `fp-ts@2.x` APIs
34+
1. Optional: activate the `@obsolete` rule for `tslint` to get guidance on what to change
35+
1. Familiarise yourself with [the new API](https://github.com/gcanti/fp-ts/pull/881)
36+
1. Gradually replace the existing code with the new API
37+
1. Upgrade to `fp-ts@2.x` and make sure to also upgrade all dependencies that rely on `fp-ts`
38+
39+
### tslint rule
40+
41+
In order to make easier to spot all the occurrences of chainable APIs without depending on `@deprecated`, which would force you to migrate in one shot, a custom tslint rule is provided (`@obsolete`).
42+
43+
Add the following lines to your `tslint.json` to turn the `@obsolete` rule on:
44+
45+
```diff
46+
{
47+
+ "rulesDirectory": ["./node_modules/fp-ts/rules"],
48+
"rules": {
49+
+ "obsolete": true
50+
}
51+
}
52+
```
53+
54+
### Dependencies
55+
56+
Don't forget to update your dependencies: libraries that use `fp-ts` like [io-ts](https://github.com/gcanti/io-ts) or [monocle-ts](https://github.com/gcanti/monocle-ts) have to be upgraded to their `fp-ts@2.x` compatible versions.
57+
58+
## The new API
59+
60+
In `fp-ts@2.x` data types are no longer implemented with classes; the biggest change resulting from this is that the chainable API has been removed. As an alternative, a `pipe` function is provided, along with suitable data-last top level functions (one for each deprecated method). This is best shown with an example:
61+
62+
v1 (deprecated)
63+
{: .label .label-red .mt-5 }
64+
65+
```ts
66+
import * as O from 'fp-ts/lib/Option'
67+
68+
O.some(1)
69+
.map(n => n * 2)
70+
.chain(n === 0 ? O.none : O.some(1 / n))
71+
.filter(n => n > 1)
72+
.foldL(() => 'ko', () => 'ok')
73+
```
74+
75+
v2 (new)
76+
{: .label .label-green .mt-5 }
77+
78+
```ts
79+
import * as O from 'fp-ts/lib/Option'
80+
import { pipe } from 'fp-ts/lib/pipeable'
81+
82+
pipe(
83+
O.some(1),
84+
O.map(n => n * 2),
85+
O.chain(n === 0 ? O.none : O.some(1 / n)),
86+
O.filter(n => n > 1),
87+
O.fold(() => 'ko', () => 'ok')
88+
)
89+
```
90+
91+
We recommend to use `pipe` even if you work with just one function, as it allows TypeScript to infer the types automatically. It's also easier to migrate existing code, because the argument order remains the same.
92+
93+
```ts
94+
import * as O from 'fp-ts/lib/Option'
95+
import { pipe } from 'fp-ts/lib/pipeable'
96+
97+
pipe(O.some(1), O.fold(() => 'ko', n => `ok: ${n}`))
98+
```
99+
100+
If you are interested, read about the [benefits of the new API](https://github.com/gcanti/fp-ts/issues/823#issuecomment-486066792) in the technical discussion leading to `fp-ts@2.x`.
101+
102+
### Replacement of the `run()` method
103+
104+
The `run()` method on `IO`, `Task`, etc. has been replaced with a thunk:
105+
106+
v1 (deprecated)
107+
{: .label .label-red .mt-5 }
108+
109+
```ts
110+
import { Task } from 'fp-ts/lib/Task'
111+
112+
const deepThought = new Task<number>(() => Promise.resolve(42))
113+
114+
deepThought.run().then(n => {
115+
console.log(`The answer is ${n}.`)
116+
})
117+
```
118+
119+
v2 (new)
120+
{: .label .label-green .mt-5 }
121+
122+
```ts
123+
import { Task } from 'fp-ts/lib/Task'
124+
125+
const deepThought: Task<number> = () => Promise.resolve(42)
126+
127+
deepThought().then(n => {
128+
console.log(`The answer is ${n}.`)
129+
})
130+
```
131+
132+
### Functions accepting fallback values are now always lazy
133+
134+
In many places `fp-ts@1.x` provided two versions of methods:
135+
136+
v1 (deprecated)
137+
{: .label .label-red .mt-5 }
138+
139+
```ts
140+
import * as O from 'fp-ts/lib/Option'
141+
142+
O.some(1).getOrElse(0) // Direct
143+
O.some(1).getOrElseL(() => 0) // Lazy, i.e. only run if needed
144+
```
145+
146+
v2 (new)
147+
{: .label .label-green .mt-5 }
148+
149+
In `fp-ts@2.x` the API has been simplified, only the lazy variants have been kept with the `L` suffix removed.
150+
151+
```ts
152+
import * as O from 'fp-ts/lib/Option'
153+
import { pipe } from 'fp-ts/lib/pipeable'
154+
155+
pipe(
156+
O.some(1),
157+
O.getOrElse(() => 0)
158+
)
159+
```
160+
161+
### Removed modules
162+
163+
- `Exception`
164+
- `Free`
165+
- `FreeGroup`
166+
- `IxIO`
167+
- `IxMonad`
168+
- `Monoidal`
169+
- `Pair`
170+
- `StrMap` (use [Record](../modules/Record.ts) instead)
171+
- `Trace`
172+
- `Validation` (use [Either](../modules/Either.ts)'s `getValidation`)
173+
- `Zipper`
174+
175+
## References
176+
177+
If you're interested in reading up on how this release came to be, have a look at the following discussions:
178+
179+
- The technical [discussion leading to v2](https://github.com/gcanti/fp-ts/issues/823)
180+
- [Version 1.19 (backport)](https://github.com/gcanti/fp-ts/pull/881)
181+
- [The 2.0.0 release](https://github.com/gcanti/fp-ts/commit/7bda18e34eed996a08afdd6a0a61025087f99593)

0 commit comments

Comments
 (0)