Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+21 added−5 removed
other:+21 added−5 removed
Provenance
Commit
512256b
Tree
abf1b42
Author
Alec Larson <aleclarson@users.noreply.github.com>
Committer
aleclarson <alec.stanford.larson@gmail.com>
Parent
18a25b8
2 files changed+21 added−5 removed
modified:src/immer.d.ts+19 added-5 removed
1
2
3
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
6
20
21
22
23
24
124 unmodified lines
Expand all
// Mapped type to remove readonly modifiers from state
// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L129
export type DraftObject<T> = {-readonly [P in keyof T]: Draft<T[P]>}
type AtomicObject = Function | Date | RegExp | Boolean | Number | String
/** Use type inference to know when an array is finite */
type IsFinite<T extends any[]> = T extends never[]
? true
: T extends ReadonlyArray<infer U> ? (U[] extends T ? false : true) : true
export type DraftObject<T> = T extends object
? T extends AtomicObject ? T : {-readonly [P in keyof T]: Draft<T[P]>}
: T
export type DraftArray<T> = Array<
T extends ReadonlyArray<any>
? {[P in keyof T]: Draft<T>}[keyof T]
: DraftObject<T>
>
export type Draft<T> = T extends any[]
? DraftArray<T[number]>
?IsFinite<T> extends true ? T :DraftArray<T[number]>
: T extends ReadonlyArray<any>
? DraftArray<T[number]>
: T extends object ? DraftObject<T> : T
124 unmodified lines
Expand all
modified:readme.md+2 added
497 unmodified lines
498```499500This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!501502+Using TypeScript v2.8.x and lower is _not_ supported by Immer.503+504## Immer on older JavaScript environments?505506By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.507