Skip to content
Myr

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

5 files+141 added−15 removed

added:__tests__/test.tsconfig.json+5 added
added:__tests__/types.ts+68 added
modified:src/immer.d.ts+23 added-11 removed
modified:package.json+21 added-4 removed
37 unmodified lines
38 "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 lines
67 "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 lines
80 ]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 lines
289## TypeScript or Flow290291The 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.292293The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:294295```ts296import produce from 'immer';297298interface State {299 readonly x: number;300}301302// `x` cannot be modified here303const state: State = {304 x: 0;305};306307const newState = produce<State>(draft => {308 // `x` can be modified here309 draft.x++;310});311312// `newState.x` cannot be modified here313```314315This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!316317## Immer on older JavaScript environments?318319By 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