Merge pull request #161 from knpwrs/mapped-draft-type
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add TypeScript Mapped Draft Type
authored by Michel Weststrate · committed by GitHub · parents 6385c50, 1978d62 · Browse files · 5 files·+141 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+141 added−15 removed
- tests:+73 added−0 removed
- other:+68 added−15 removed
Provenance
- Commit
edc8b7c- Tree
920a768- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
6385c50- Parent 2
1978d62
5 files changed+141 added−15 removed
added:__tests__/test.tsconfig.json+5 added
12345{ "compilerOptions": { "module": "commonjs" }}added:__tests__/types.ts+68 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import produce from '../src/immer';
interface State { readonly num: number; readonly foo?: string; bar: string; readonly baz: { readonly x: number; readonly y: number; }; readonly arr: ReadonlyArray<{ readonly value: string }>; readonly arr2: { readonly value: string }[];}
const state: State = { num: 0, bar: 'foo', baz: { x: 1, y: 2, }, arr: [{ value: 'asdf' }], arr2: [{ value: 'asdf' }],};
const expectedState: State = { num: 1, foo: 'bar', bar: 'foo', baz: { x: 2, y: 3, }, arr: [{ value: 'foo' }, { value: 'asf' }], arr2: [{ value: 'foo' }, { value: 'asf' }],};
it('can update readonly state via standard api', () => { const newState = produce<State>(state, draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); }); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});
it('can update readonly state via curried api', () => { const newState = produce<State>(draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); })(state); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});modified:src/immer.d.ts+23 added-11 removed
123456789101112131415167 unmodified lines2425262716282930313233223435363738273940414243324445464748374950515253545544565758596061625163646566556768697059717273746375767778798069818283848510 unmodified lines// 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-L129export type DraftObject<T> = { -readonly [P in keyof T]: Draft<T[P]>;};export interface DraftArray<T> extends Array<Draft<T>> { }export type Draft<T> = T extends any[] ? DraftArray<T[number]> : T extends ReadonlyArray<any> ? DraftArray<T[number]> : T extends object ? DraftObject<T> : T;
/** * Immer takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.7 unmodified lines * @returns The next state: a new state, or the current state if nothing was modified */export default function<S = any>( currentState: S, recipe: (this: S, draftState: S) => void | S recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): S
// curried invocations with default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, initialState: S): (currentState: S | undefined) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S, initialState: S): (currentState: S | undefined, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S, initialState: S): (currentState: S | undefined, ...extraArgs: any[]) => S
// curried invocations without default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S) => void | S recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (currentState: S) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S): (currentState: S, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S): (currentState: S, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S): (currentState: S, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S): (currentState: S, ...extraArgs: any[]) => S/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function.10 unmodified linesmodified:package.json+21 added-4 removed
37 unmodified lines38 "files": [39 "dist/"40 ],41 "devDependencies": {42- "@types/jest": "^22.0.0",42+ "@types/jest": "^22.2.3",43 "babel-cli": "^6.26.0",44 "babel-core": "^6.26.0",45 "babel-jest": "^22.0.4",46 "babel-plugin-external-helpers": "^6.22.0",20 unmodified lines67 "rollup-plugin-commonjs": "^8.2.6",68 "rollup-plugin-filesize": "^1.5.0",69 "rollup-plugin-node-resolve": "^3.0.2",70 "rollup-plugin-uglify": "^2.0.1",71+ "ts-jest": "^22.4.6",71- "typescript": "^2.6.2",72+ "typescript": "^2.9.1",73 "uglify-es": "^3.3.6",74 "yarn-or-npm": "^2.0.4"75 },76 "lint-staged": {3 unmodified lines80 ]81 },82 "jest": {83 "transform": {83- "^.+\\.jsx?$": "babel-jest"84- }84+ "^.+\\.jsx?$": "babel-jest",85+ "^.+\\.tsx?$": "ts-jest"86+ },87+ "testRegex": "/__tests__/[^/]*[jt]sx?$",88+ "globals": {89+ "ts-jest": {90+ "enableTsDiagnostics": true,91+ "tsConfigFile": "__tests__/test.tsconfig.json"92+ }93+ },94+ "moduleFileExtensions": [95+ "ts",96+ "tsx",97+ "js",98+ "jsx",99+ "json",100+ "node"101+ ]102 }103 }modified:readme.md+24 added
288 unmodified lines289 ## TypeScript or Flow290 291 The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.292 293+The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:294+295+```ts296+import produce from 'immer';297+298+interface State {299+ readonly x: number;300+}301+302+// `x` cannot be modified here303+const state: State = {304+ x: 0;305+};306+307+const newState = produce<State>(draft => {308+ // `x` can be modified here309+ draft.x++;310+});311+312+// `newState.x` cannot be modified here313+```314+315+This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!316+317 ## Immer on older JavaScript environments?318 319 By 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.320 132 unmodified lines