fix(ts): improve IProduce
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #205
authored by Alec Larson · committed by aleclarson · parent b46f696 · Browse files · 1 file·+25 added−75 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−75 removed
- other:+25 added−75 removed
Provenance
- Commit
8a86e7c- Tree
09bb7bb- Author
Alec Larson <aleclarson@users.noreply.github.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
b46f696
1 file changed+25 added−75 removed
modified:src/immer.d.ts+25 added-75 removed
13 unmodified lines1415161718181920212221232425262728295 unmodified lines3536373835394041423738434445464748495015 unmodified lines666768696270716472736674756869707176777879737475767778798081828384858687888990919293949596979899100101102103104105808110710810911011111211311411511611711811912012182838485861251261271281291301311321331341351361371381398788899091929322 unmodified lines13 unmodified lines
/** 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 : 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 extends AtomicObject ? T : {-readonly [P in keyof T]: Draft<T[P]>} : T
export type DraftArray<T> = Array< T extends ReadonlyArray<any>5 unmodified lines [P in keyof T]: T[P] extends T[number] ? Draft<T[P]> : never}
export type Draft<T> = T extends any[] ? IsFinite<T> extends true ? DraftTuple<T> : DraftArray<T[number]> ? IsFinite<T> extends true ? DraftTuple<T> : DraftArray<T[number]> : T extends ReadonlyArray<any> ? DraftArray<T[number]> : T extends object ? DraftObject<T> : T ? DraftArray<T[number]> : T extends object ? DraftObject<T> : T
export interface Patch { op: "replace" | "remove" | "add" path: (string | number)[]15 unmodified lines * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined * @returns The next state: a new state, or the current state if nothing was modified */ <S = any>( <S = any, R = never>( currentState: S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | R, listener?: PatchListener ): S ): R
// curried invocations with default initial state // 0 additional arguments <S = any>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, /** Curried producer with an initial state */ <S = any, R = never>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | R, initialState: S ): (currentState: S | undefined) => S // 1 additional argument of type A <S = any, A = any>( 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 B <S = any, A = any, B = any>( 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 C <S = any, A = any, B = any, C = any>( 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/5453 <S = any>( recipe: ( this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[] ) => void | S, initialState: S ): (currentState: S | undefined, ...extraArgs: any[]) => S ): (currentState: S | undefined) => R
// curried invocations without default initial state // 0 additional arguments <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): ( currentState: S ) => S // 1 additional argument of type A <S = any, A = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S ): (currentState: S, a: A) => S // 2 additional arguments of types A and B <S = any, A = any, B = any>( 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 C <S = any, A = any, B = any, C = any>( /** Curried producer with no initial state */ <S = any, R = never, Args extends any[] = any[]>( 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/5453 <S = any>( recipe: ( this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[] ) => void | S ): (currentState: S, ...extraArgs: any[]) => S ...extraArgs: Args ) => void | R ): (currentState: S, ...extraArgs: Args) => R}
export const produce: IProduceexport default produce22 unmodified lines