Comparing version 8.0.3 to 9.0.0-beta.0
@@ -49,3 +49,3 @@ import { IProduceWithPatches, IProduce, ImmerState, Drafted, Patch, Objectish, Draft, PatchListener } from "../internal"; | ||
setUseProxies(value: boolean): void; | ||
applyPatches(base: Objectish, patches: Patch[]): any; | ||
applyPatches(base: Objectish, patches: Patch[]): Objectish; | ||
} | ||
@@ -52,0 +52,0 @@ export declare function createProxy<T extends Objectish>(immer: Immer, value: T, parent?: ImmerState): Drafted<T, ImmerState>; |
import { Nothing } from "../internal"; | ||
declare type Tail<T extends any[]> = ((...t: T) => any) extends (_: any, ...tail: infer TT) => any ? TT : []; | ||
declare type AnyFunc = (...args: any[]) => any; | ||
declare type PrimitiveType = number | string | boolean; | ||
@@ -38,2 +38,16 @@ /** Object types that should never be mapped */ | ||
/** | ||
* Utility types | ||
*/ | ||
declare type PatchesTuple<T> = readonly [T, Patch[], Patch[]]; | ||
declare type ValidRecipeReturnType<State> = State | void | undefined | (State extends undefined ? Nothing : never); | ||
declare type ValidRecipeReturnTypePossiblyPromise<State> = ValidRecipeReturnType<State> | Promise<ValidRecipeReturnType<State>>; | ||
declare type PromisifyReturnIfNeeded<State, Recipe extends AnyFunc, UsePatches extends boolean> = ReturnType<Recipe> extends Promise<any> ? Promise<UsePatches extends true ? PatchesTuple<State> : State> : UsePatches extends true ? PatchesTuple<State> : State; | ||
/** | ||
* Core Producer inference | ||
*/ | ||
declare type InferRecipeFromCurried<Curried> = Curried extends (base: infer State, ...rest: infer Args) => any ? ReturnType<Curried> extends State ? (draft: Draft<State>, ...rest: Args) => ValidRecipeReturnType<Draft<State>> : never : never; | ||
declare type InferInitialStateFromCurried<Curried> = Curried extends (base: infer State, ...rest: any[]) => any ? State : never; | ||
declare type InferCurriedFromRecipe<Recipe, UsePatches extends boolean> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any ? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState> ? (base: Immutable<DraftState>, ...args: RestArgs) => PromisifyReturnIfNeeded<Immutable<DraftState>, Recipe, UsePatches> : never : never; | ||
declare type InferCurriedFromInitialStateAndRecipe<State, Recipe, UsePatches extends boolean> = Recipe extends (draft: Draft<State>, ...rest: infer RestArgs) => ValidRecipeReturnTypePossiblyPromise<State> ? (base?: State | undefined, ...args: RestArgs) => PromisifyReturnIfNeeded<State, Recipe, UsePatches> : never; | ||
/** | ||
* The `produce` function takes a value and a "recipe function" (whose | ||
@@ -58,8 +72,13 @@ * return value often depends on the base state). The recipe function is | ||
export interface IProduce { | ||
/** Curried producer */ | ||
<Recipe extends (...args: any[]) => any, Params extends any[] = Parameters<Recipe>, T = Params[0]>(recipe: Recipe): <Base extends Immutable<T>>(base: Base, ...rest: Tail<Params>) => Produced<Base, ReturnType<Recipe>>; | ||
/** Curried producer with initial state */ | ||
<Recipe extends (...args: any[]) => any, Params extends any[] = Parameters<Recipe>, T = Params[0]>(recipe: Recipe, initialState: Immutable<T>): <Base extends Immutable<T>>(base?: Base, ...rest: Tail<Params>) => Produced<Base, ReturnType<Recipe>>; | ||
/** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */ | ||
<Curried>(recipe: InferRecipeFromCurried<Curried>, initialState?: InferInitialStateFromCurried<Curried>): Curried; | ||
/** Curried producer that infers curried from the recipe */ | ||
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, false>; | ||
/** Curried producer with initial state, infers recipe from initial state */ | ||
<State, Recipe extends Function>(recipe: Recipe, initialState: State): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>; | ||
/** Promisified dormal producer */ | ||
<Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>, listener?: PatchListener): Promise<Base>; | ||
/** Normal producer */ | ||
<Base, D = Draft<Base>, Return = void>(base: Base, recipe: (draft: D) => Return, listener?: PatchListener): Produced<Base, Return>; | ||
<Base, D = Draft<Base>>(// By using a default inferred D, rather than Draft<Base> in the recipe, we can override it. | ||
base: Base, recipe: (draft: D) => ValidRecipeReturnType<D>, listener?: PatchListener): Base; | ||
} | ||
@@ -73,8 +92,6 @@ /** | ||
export interface IProduceWithPatches { | ||
/** Curried producer */ | ||
<Recipe extends (...args: any[]) => any, Params extends any[] = Parameters<Recipe>, T = Params[0]>(recipe: Recipe): <Base extends Immutable<T>>(base: Base, ...rest: Tail<Params>) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]; | ||
/** Curried producer with initial state */ | ||
<Recipe extends (...args: any[]) => any, Params extends any[] = Parameters<Recipe>, T = Params[0]>(recipe: Recipe, initialState: Immutable<T>): <Base extends Immutable<T>>(base?: Base, ...rest: Tail<Params>) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]; | ||
/** Normal producer */ | ||
<Base, D = Draft<Base>, Return = void>(base: Base, recipe: (draft: D) => Return): [Produced<Base, Return>, Patch[], Patch[]]; | ||
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>; | ||
<State, Recipe extends Function>(recipe: Recipe, initialState: State): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>; | ||
<Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => ValidRecipeReturnType<Base>, listener?: PatchListener): PatchesTuple<Immutable<Base>>; | ||
<Base, D = Draft<Base>>(base: Base, recipe: (draft: D) => Promise<ValidRecipeReturnType<Base>>, listener?: PatchListener): PatchesTuple<Promise<Immutable<Base>>>; | ||
} | ||
@@ -81,0 +98,0 @@ export declare function never_used(): void; |
{ | ||
"name": "immer", | ||
"version": "8.0.3", | ||
"version": "9.0.0-beta.0", | ||
"description": "Create your next immutable state by mutating the current one", | ||
@@ -95,3 +95,3 @@ "main": "dist/index.js", | ||
"tsdx": "^0.12.3", | ||
"typescript": "^3.9.3", | ||
"typescript": "^4.2.3", | ||
"webpack": "^4.41.6", | ||
@@ -98,0 +98,0 @@ "webpack-cli": "^3.3.11" |
import {Nothing} from "../internal" | ||
type Tail<T extends any[]> = ((...t: T) => any) extends ( | ||
_: any, | ||
...tail: infer TT | ||
) => any | ||
? TT | ||
: [] | ||
type AnyFunc = (...args: any[]) => any | ||
@@ -88,2 +83,75 @@ type PrimitiveType = number | string | boolean | ||
/** | ||
* Utility types | ||
*/ | ||
type PatchesTuple<T> = readonly [T, Patch[], Patch[]] | ||
type ValidRecipeReturnType<State> = | ||
| State | ||
| void | ||
| undefined | ||
| (State extends undefined ? Nothing : never) | ||
type ValidRecipeReturnTypePossiblyPromise<State> = | ||
| ValidRecipeReturnType<State> | ||
| Promise<ValidRecipeReturnType<State>> | ||
type PromisifyReturnIfNeeded< | ||
State, | ||
Recipe extends AnyFunc, | ||
UsePatches extends boolean | ||
> = ReturnType<Recipe> extends Promise<any> | ||
? Promise<UsePatches extends true ? PatchesTuple<State> : State> | ||
: UsePatches extends true | ||
? PatchesTuple<State> | ||
: State | ||
/** | ||
* Core Producer inference | ||
*/ | ||
type InferRecipeFromCurried<Curried> = Curried extends ( | ||
base: infer State, | ||
...rest: infer Args | ||
) => any // extra assertion to make sure this is a proper curried function (state, args) => state | ||
? ReturnType<Curried> extends State | ||
? ( | ||
draft: Draft<State>, | ||
...rest: Args | ||
) => ValidRecipeReturnType<Draft<State>> | ||
: never | ||
: never | ||
type InferInitialStateFromCurried<Curried> = Curried extends ( | ||
base: infer State, | ||
...rest: any[] | ||
) => any // extra assertion to make sure this is a proper curried function (state, args) => state | ||
? State | ||
: never | ||
type InferCurriedFromRecipe< | ||
Recipe, | ||
UsePatches extends boolean | ||
> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type | ||
? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState> | ||
? ( | ||
base: Immutable<DraftState>, | ||
...args: RestArgs | ||
) => PromisifyReturnIfNeeded<Immutable<DraftState>, Recipe, UsePatches> | ||
: never // incorrect return type | ||
: never // not a function | ||
type InferCurriedFromInitialStateAndRecipe< | ||
State, | ||
Recipe, | ||
UsePatches extends boolean | ||
> = Recipe extends ( | ||
draft: Draft<State>, | ||
...rest: infer RestArgs | ||
) => ValidRecipeReturnTypePossiblyPromise<State> | ||
? ( | ||
base?: State | undefined, | ||
...args: RestArgs | ||
) => PromisifyReturnIfNeeded<State, Recipe, UsePatches> | ||
: never // recipe doesn't match initial state | ||
/** | ||
* The `produce` function takes a value and a "recipe function" (whose | ||
@@ -108,36 +176,33 @@ * return value often depends on the base state). The recipe function is | ||
export interface IProduce { | ||
/** Curried producer */ | ||
< | ||
Recipe extends (...args: any[]) => any, | ||
Params extends any[] = Parameters<Recipe>, | ||
T = Params[0] | ||
>( | ||
recipe: Recipe | ||
): <Base extends Immutable<T>>( | ||
base: Base, | ||
...rest: Tail<Params> | ||
) => Produced<Base, ReturnType<Recipe>> | ||
// ^ by making the returned type generic, the actual type of the passed in object is preferred | ||
// over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe | ||
// Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct! | ||
/** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */ | ||
<Curried>( | ||
recipe: InferRecipeFromCurried<Curried>, | ||
initialState?: InferInitialStateFromCurried<Curried> | ||
): Curried | ||
/** Curried producer with initial state */ | ||
< | ||
Recipe extends (...args: any[]) => any, | ||
Params extends any[] = Parameters<Recipe>, | ||
T = Params[0] | ||
>( | ||
/** Curried producer that infers curried from the recipe */ | ||
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe< | ||
Recipe, | ||
false | ||
> | ||
/** Curried producer with initial state, infers recipe from initial state */ | ||
<State, Recipe extends Function>( | ||
recipe: Recipe, | ||
initialState: Immutable<T> | ||
): <Base extends Immutable<T>>( | ||
base?: Base, | ||
...rest: Tail<Params> | ||
) => Produced<Base, ReturnType<Recipe>> | ||
initialState: State | ||
): InferCurriedFromInitialStateAndRecipe<State, Recipe, false> | ||
/** Promisified dormal producer */ | ||
<Base, D = Draft<Base>>( | ||
base: Base, | ||
recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>, | ||
listener?: PatchListener | ||
): Promise<Base> | ||
/** Normal producer */ | ||
<Base, D = Draft<Base>, Return = void>( | ||
<Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it. | ||
base: Base, | ||
recipe: (draft: D) => Return, | ||
recipe: (draft: D) => ValidRecipeReturnType<D>, | ||
listener?: PatchListener | ||
): Produced<Base, Return> | ||
): Base | ||
} | ||
@@ -152,35 +217,18 @@ | ||
export interface IProduceWithPatches { | ||
/** Curried producer */ | ||
< | ||
Recipe extends (...args: any[]) => any, | ||
Params extends any[] = Parameters<Recipe>, | ||
T = Params[0] | ||
>( | ||
recipe: Recipe | ||
): <Base extends Immutable<T>>( | ||
base: Base, | ||
...rest: Tail<Params> | ||
) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]] | ||
// ^ by making the returned type generic, the actual type of the passed in object is preferred | ||
// over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe | ||
// Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct! | ||
/** Curried producer with initial state */ | ||
< | ||
Recipe extends (...args: any[]) => any, | ||
Params extends any[] = Parameters<Recipe>, | ||
T = Params[0] | ||
>( | ||
// Types copied from IProduce, wrapped with PatchesTuple | ||
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true> | ||
<State, Recipe extends Function>( | ||
recipe: Recipe, | ||
initialState: Immutable<T> | ||
): <Base extends Immutable<T>>( | ||
base?: Base, | ||
...rest: Tail<Params> | ||
) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]] | ||
/** Normal producer */ | ||
<Base, D = Draft<Base>, Return = void>( | ||
initialState: State | ||
): InferCurriedFromInitialStateAndRecipe<State, Recipe, true> | ||
<Base, D = Draft<Base>>( | ||
base: Base, | ||
recipe: (draft: D) => Return | ||
): [Produced<Base, Return>, Patch[], Patch[]] | ||
recipe: (draft: D) => ValidRecipeReturnType<Base>, | ||
listener?: PatchListener | ||
): PatchesTuple<Immutable<Base>> | ||
<Base, D = Draft<Base>>( | ||
base: Base, | ||
recipe: (draft: D) => Promise<ValidRecipeReturnType<Base>>, | ||
listener?: PatchListener | ||
): PatchesTuple<Promise<Immutable<Base>>> | ||
} | ||
@@ -187,0 +235,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
836531
6557
1