feat(ts): Support typescript 3.4
Before adoption: this commit came in with the history the project adopted, so no landing records it.
BREAKING CHANGE: typescript@3.4.0 is now the minimum required version
authored by Alec Larson · committed by Alec Larson · parent 2c27133 · Browse files · 5 files·+92 added−55 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+92 added−55 removed
- tests:+66 added−39 removed
- other:+26 added−16 removed
Provenance
- Commit
2868065- Tree
58605f9- Author
Alec Larson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
2c27133
5 files changed+92 added−55 removed
modified:__tests__/immutable.ts+5 added-5 removed
12 unmodified lines1314151617171819202122232324252627282929303132333 unmodified lines373839404141424344454647474849505115 unmodified lines12 unmodified lines
// array in tuple{ let val = {} as Immutable<[string[], 1]> exactType(val, {} as [ReadonlyArray<string>, 1]) exactType(val, {} as readonly [ReadonlyArray<string>, 1])}
// tuple in array{ let val = {} as Immutable<[string, 1][]> exactType(val, {} as ReadonlyArray<[string, 1]>) exactType(val, {} as ReadonlyArray<readonly [string, 1]>)}
// tuple in tuple{ let val = {} as Immutable<[[string, 1], 1]> exactType(val, {} as [[string, 1], 1]) exactType(val, {} as readonly [readonly [string, 1], 1])}
// array in array{3 unmodified lines
// tuple in object{ let val = {} as Immutable<{a: [string, 1]}> exactType(val, {} as {readonly a: [string, 1]}) exactType(val, {} as {readonly a: readonly [string, 1]})}
// object in tuple{ let val = {} as Immutable<[{a: string}, 1]> exactType(val, {} as [{readonly a: string}, 1]) exactType(val, {} as readonly [{readonly a: string}, 1])}
// array in object{15 unmodified linesmodified:__tests__/produce.ts+61 added-34 removed
1 import produce, {2 produce as produce2,3 applyPatches,4 Patch,5- nothing5+ nothing,6+ Draft7 } from "../dist/immer.js"8 9 // prettier-ignore10 type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)57 unmodified lines68 })69 70 // NOTE: only when the function type is inferred71 it("can infer state type from default state", () => {71- type Producer = (base: number | undefined) => number72- let foo = produce(_ => {}, 1)73- exactType(foo, {} as Producer)74- exactType(foo(2), 0 as number)72+ type State = {readonly a: number} | boolean73+ type Recipe = (base?: State | undefined) => State74+75+ let foo = produce(_ => {}, {} as State)76+ exactType(foo, {} as Recipe)77 })78 79 it("can infer state type from recipe function", () => {78- type T = string | number79- type Producer = (base: T | undefined, _2: number) => T80+ type State = {readonly a: string} | {readonly b: string}81+ type Recipe = (base: State | undefined, arg: number) => State82 81- let foo = produce((_: string | number, _2: number) => {}, 1)82- exactType(foo, {} as Producer)83- exactType(foo("", 0), {} as string | number)83+ let foo = produce((draft: Draft<State>, arg: number) => {}, {} as any)84+ exactType(foo, {} as Recipe)85 })86 87 it("cannot infer state type when the function type and default state are missing", () => {88+ const res = produce(_ => {})87- exactType(produce(_ => {}), {} as (base: any) => any)89+ exactType(res, {} as (base: any) => any)90 })91 92 it("can update readonly state via curried api", () => {93 const newState = produce<State>(draft => {43 unmodified lines137 })138 139 describe("curried producer", () => {140 it("supports rest parameters", () => {139- type Foo = (base: {}, _2: number, _3: number) => {}140- let foo = produce((_1: {}, _2: number, _3: number) => {})141- exactType(foo, {} as Foo)142- foo({}, 1, 2)141+ type State = {readonly a: 1}142 143+ // No initial state:144+ let foo = produce<State, number[]>(() => {})145+ exactType(foo, {} as (base: State, ...args: number[]) => State)146+ foo({} as State, 1, 2)147+148+ // TODO: Using argument parameters149+ // let woo = produce((state: Draft<State>, ...args: number[]) => {})150+ // exactType(woo, {} as (base: State, ...args: number[]) => State)151+ // woo({} as State, 1, 2)152+153 // With initial state:145- type Bar = (base: {} | undefined, _2: number, _3: number) => {}146- let bar = produce((_1: {}, _2: number, _3: number) => {}, {})147- exactType(bar, {} as Bar)148- bar(undefined, 1, 2)154+ let bar = produce(155+ (state: Draft<State>, ...args: number[]) => {},156+ {} as State157+ )158+ exactType(bar, {} as (base?: State, ...args: number[]) => State)159+ bar({} as State | undefined, 1, 2)160+ bar()161+162+ // When args is a tuple:163+ let tup = produce(164+ (state: Draft<State>, ...args: [string, ...number[]]) => {},165+ {} as State166+ )167+ exactType(tup, {} as (168+ base: State | undefined,169+ arg1: string,170+ ...args: number[]171+ ) => State)172+ tup({a: 1}, "", 2)173+ tup(undefined, "", 2)174 })175 176 it("can be passed a readonly array", () => {152- let foo = produce((_: any[]) => {})177+ // No initial state:178+ let foo = produce<ReadonlyArray<any>>(() => {})179+ exactType(foo, {} as (base: readonly any[]) => readonly any[])180 foo([] as ReadonlyArray<any>)181 182 // With initial state:156- let bar = produce((_: any[]) => {}, [])183+ let bar = produce(() => {}, [] as ReadonlyArray<any>)184+ exactType(bar, {} as (base?: readonly any[]) => readonly any[])185 bar([] as ReadonlyArray<any>)186+ bar(undefined)187+ bar()188 })189 })190 191 it("works with return type of: number", () => {56 unmodified lines248 exactType(result, {} as {a: number} | undefined)249 })250 251 it("can return a promise", () => {222- let base = {} as {readonly a: number}252+ type Base = {readonly a: number}253+ let base = {} as Base254 255 // Return a promise only.225- exactType(226- produce(base, draft => {227- return Promise.resolve(draft.a > 0 ? null : undefined)228- }),229- {} as Promise<{readonly a: number} | null>256+ let res1 = produce(base, draft => {257+ return Promise.resolve(draft.a > 0 ? null : undefined)258+ })259+ exactType(res1, {} as Promise<Base | null>)230- )260 261 // Return a promise or undefined.233- exactType(234- produce(base, draft => {235- if (draft.a > 0) return Promise.resolve()236- }),237- {} as (Promise<{readonly a: number}> | {readonly a: number})262+ let res2 = produce(base, draft => {263+ if (draft.a > 0) return Promise.resolve()264+ })265+ exactType(res2, {} as Base | Promise<Base>)238- )266 })267 268 it("works with `void` hack", () => {269 let base = {} as {readonly a: number}17 unmodified linesmodified:src/immer.d.ts+21 added-11 removed
90 unmodified lines91 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified92 * @param {Function} patchListener - optional function that will be called with all the patches produced here93 * @returns {any} a new state, or the initial state if nothing was modified94 */95- <T = any, Return = void, D = Draft<T>>(96- base: T,97- recipe: (this: D, draft: D) => Return,95+ <Base = any, Return = void>(96+ base: Base extends Function ? never : Base,97+ recipe: (this: Draft<Base>, draft: Draft<Base>) => Return,98 listener?: PatchListener99- ): Produced<T, Return>99+ ): Produced<Base, Return>100 101 /** Curried producer with a default value */102- <T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(103- recipe: (this: D, draft: D, ...rest: Rest) => Return,104- defaultBase: T105- ): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>102+ <Base = any, Rest extends any[] = [], Return = void>(103+ recipe: (this: Base, draft: Base, ...rest: Rest) => Return,104+ defaultBase: Immutable<Base>105+ ): Rest[number][] extends Rest | never[]106+ ? (107+ // The `base` argument is optional when `Rest` is optional.108+ base?: Immutable<Base>,109+ ...rest: Rest110+ ) => Produced<Immutable<Base>, Return>111+ : (112+ // The `base` argument is required when `Rest` is required.113+ base: Immutable<Base> | undefined,114+ ...rest: Rest115+ ) => Produced<Immutable<Base>, Return>116 117 /** Curried producer with no default value */108- <T = any, Rest extends any[] = [], Return = void>(109- recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return110- ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>118+ <Base = any, Rest extends any[] = [], Return = void>(119+ recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return120+ ): (base: Immutable<Base>, ...rest: Rest) => Produced<Base, Return>121 }122 123 export const produce: IProduce124 export default produce138 unmodified linesmodified:package.json+1 added-1 removed
65 unmodified lines666768697070717273748 unmodified lines65 unmodified lines "pretty-quick": "^1.8.0", "regenerator-runtime": "^0.11.1", "rimraf": "^2.6.2", "seamless-immutable": "^7.1.3", "typescript": "3.1.1", "typescript": "3.4.3", "yarn-or-npm": "^2.0.4" }, "jest": { "moduleFileExtensions": [8 unmodified linesmodified:yarn.lock+4 added-4 removed
6047 unmodified lines6048 integrity sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=6049 dependencies:6050 prelude-ls "~1.1.2"6051 6052-typescript@3.1.1:6053- version "3.1.1"6054- resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.1.tgz#3362ba9dd1e482ebb2355b02dfe8bcd19a2c7c96"6055- integrity sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==6052+typescript@3.4.3:6053+ version "3.4.3"6054+ resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"6055+ integrity sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==6056 6057 uglify-js@^3.1.4:6058 version "3.5.3"6059 resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.3.tgz#d490bb5347f23025f0c1bc0dee901d98e4d6b063"301 unmodified lines