Reformat all TypeScript code with new prettier version
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Stefan Wille · committed by Stefan Wille · parent d0d2b86 · Browse files · 3 files·+124 added−103 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+124 added−103 removed
- tests:+88 added−85 removed
- other:+36 added−18 removed
Provenance
- Commit
e06b330- Tree
430aa71- Author
Stefan Wille <github@stefanwille.com>- Committer
Stefan Wille <github@stefanwille.com>- Parent
d0d2b86
3 files changed+124 added−103 removed
modified:__tests__/flow/ts.ts+4 added-4 removed
77 unmodified lines7879808182828384858687888990919292939495969596979877 unmodified lines : state}
produce({x: 3, z: {}}, draftState => { const a = draftState; const a = draftState
if (a) { a.x // $ExpectError a.y }})
produce([1], draftState => { const a = original(draftState); const a = original(draftState) if (a) { // $ExpectError const b: string = a[0]; const c: number = a[0]; const b: string = a[0] const c: number = a[0] }})modified:__tests__/types.ts+84 added-81 removed
11234567891011124567891011121314151617181920212223241617181920212223242526272829303132333435362728293031323334353637383940414243444546474849505152383940414243444546474849505152535455565758596061626364656667685455565758596061626364656667686970717273747576777879808182838485717273747576777879808182838485868786878990919293888990919293949596979899import produce, { produce as produce2, applyPatches, Patch} from '../src/immer';import produce, {produce as produce2, applyPatches, Patch} 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 }[]; 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' }],}; 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' }],}; 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 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);});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)})
it("can update use the non-default export", () => { const newState = produce2<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)})
it('can update use the non-default export', () => { const newState = produce2<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);});
it('can apply patches', () => {it("can apply patches", () => { let patches: Patch[] produce({ x: 3 }, d=> { d.x++ }, (p) => { patches = p }) produce( {x: 3}, d => { d.x++ }, p => { patches = p } )
expect(applyPatches({}, patches)).toEqual({x: 4})})modified:src/immer.d.ts+36 added-18 removed
1 // Mapped type to remove readonly modifiers from state2 // Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L1293-export type DraftObject<T> = {4- -readonly [P in keyof T]: Draft<T[P]>;5-};6-export interface DraftArray<T> extends Array<Draft<T>> { }7-export type Draft<T> =8- T extends any[] ? DraftArray<T[number]> :9- T extends ReadonlyArray<any> ? DraftArray<T[number]> :10- T extends object ? DraftObject<T> :11- T;3+export type DraftObject<T> = {-readonly [P in keyof T]: Draft<T[P]>}4+export interface DraftArray<T> extends Array<Draft<T>> {}5+export type Draft<T> = T extends any[]6+ ? DraftArray<T[number]>7+ : T extends ReadonlyArray<any>8+ ? DraftArray<T[number]>9+ : T extends object ? DraftObject<T> : T10 11 export interface Patch {14- op: "replace" | "remove" | "add",15- path: (string|number)[]12+ op: "replace" | "remove" | "add"13+ path: (string | number)[]14 value?: any15 }16 17 export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void35 unmodified lines53 initialState: S54 ): (currentState: S | undefined, a: A, b: B) => S55 // 3 additional arguments of types A, B and C56 <S = any, A = any, B = any, C = any>(59- recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,57+ recipe: (58+ this: Draft<S>,59+ draftState: Draft<S>,60+ a: A,61+ b: B,62+ c: C63+ ) => void | S,64 initialState: S65 ): (currentState: S | undefined, a: A, b: B, c: C) => S66 // any number of additional arguments, but with loss of type safety67 // this may be alleviated if "variadic kinds" makes it into Typescript:68 // https://github.com/Microsoft/TypeScript/issues/545369 <S = any>(66- recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,70+ recipe: (71+ this: Draft<S>,72+ draftState: Draft<S>,73+ ...extraArgs: any[]74+ ) => void | S,75 initialState: S76 ): (currentState: S | undefined, ...extraArgs: any[]) => S77 78 // curried invocations without default initial state79 // 0 additional arguments72- <S = any>(73- recipe: (this: Draft<S>, draftState: Draft<S>) => void | S74- ): (currentState: S) => S80+ <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (81+ currentState: S82+ ) => S83 // 1 additional argument of type A84 <S = any, A = any>(85 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S86 ): (currentState: S, a: A) => S2 unmodified lines89 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S90 ): (currentState: S, a: A, b: B) => S91 // 3 additional arguments of types A, B and C92 <S = any, A = any, B = any, C = any>(85- recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S93+ recipe: (94+ this: Draft<S>,95+ draftState: Draft<S>,96+ a: A,97+ b: B,98+ c: C99+ ) => void | S100 ): (currentState: S, a: A, b: B, c: C) => S101 // any number of additional arguments, but with loss of type safety102 // this may be alleviated if "variadic kinds" makes it into Typescript:103 // https://github.com/Microsoft/TypeScript/issues/5453104 <S = any>(91- recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S105+ recipe: (106+ this: Draft<S>,107+ draftState: Draft<S>,108+ ...extraArgs: any[]109+ ) => void | S110 ): (currentState: S, ...extraArgs: any[]) => S111 }112 113 export const produce: IProduce21 unmodified lines