Comparing version 1.9.2 to 1.9.3
@@ -15,33 +15,23 @@ /** Object types that should never be mapped */ | ||
/** 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 | ||
type ArrayMethod = Exclude<keyof [], number> | ||
type Indices<T> = Exclude<keyof T, ArrayMethod> | ||
export type DraftObject<T> = T extends object | ||
? T extends AtomicObject | ||
? T | ||
: {-readonly [P in keyof T]: Draft<T[P]>} | ||
: T | ||
export type DraftArray<T> = Array< | ||
T extends ReadonlyArray<any> | ||
? {[P in keyof T]: Draft<T>}[keyof T] | ||
: DraftObject<T> | ||
export type DraftArray<T extends ReadonlyArray<any>> = Array< | ||
{[P in Indices<T>]: Draft<T[P]>}[Indices<T>] | ||
> | ||
export type DraftTuple<T extends any[]> = { | ||
[P in keyof T]: T[P] extends T[number] ? Draft<T[P]> : never | ||
export type DraftTuple<T extends ReadonlyArray<any>> = { | ||
[P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never | ||
} | ||
export type Draft<T> = T extends any[] | ||
? IsFinite<T> extends true | ||
? DraftTuple<T> | ||
: DraftArray<T[number]> | ||
export type Draft<T> = T extends never[] | ||
? T | ||
: T extends ReadonlyArray<any> | ||
? DraftArray<T[number]> | ||
? T[number][] extends T | ||
? DraftArray<T> | ||
: DraftTuple<T> | ||
: T extends AtomicObject | ||
? T | ||
: T extends object | ||
? DraftObject<T> | ||
? {-readonly [P in keyof T]: Draft<T[P]>} | ||
: T | ||
@@ -57,2 +47,21 @@ | ||
/** Includes 1 when `void` or `undefined` exists in type `T` */ | ||
type HasVoidLike<T> = (void extends T ? 1 : 0) | (undefined extends T ? 1 : 0) | ||
/** Includes 1 when type `T` is `void` or `undefined` (or both) */ | ||
type IsVoidLike<T> = | ||
| (T extends void ? 1 : 0) | ||
| (T extends undefined ? 1 : 0) | ||
| (T extends void | undefined ? 1 : 0) | ||
/** Converts `nothing` into `undefined` */ | ||
type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T | ||
/** The inferred return type of `produce` */ | ||
type Produced<Base, Return> = 1 extends HasVoidLike<Return> | ||
? 1 extends IsVoidLike<Return> | ||
? Base | ||
: Base | FromNothing<Exclude<Return, void>> | ||
: FromNothing<Return> | ||
export interface IProduce { | ||
@@ -78,22 +87,17 @@ /** | ||
*/ | ||
<State, Result = any>( | ||
currentState: State, | ||
recipe: (this: Draft<State>, draft: Draft<State>) => Result, | ||
<Base, Proxy = Draft<Base>, Return = void>( | ||
base: Base, | ||
recipe: (this: Proxy, draft: Proxy) => Return, | ||
listener?: PatchListener | ||
): void extends Result ? State : Result | ||
): Produced<Base, Return> | ||
/** Curried producer with an initial state */ | ||
<State, Result = any>( | ||
recipe: (this: Draft<State>, draft: Draft<State>) => void | Result, | ||
defaultBase: State | ||
): (base: State | undefined) => void extends Result ? State : Result | ||
/** Curried producer with no initial state */ | ||
<State, Result = any, Args extends any[] = any[]>( | ||
/** Curried producer */ | ||
<Default = any, Base = Default, Rest extends any[] = [], Return = void>( | ||
recipe: ( | ||
this: Draft<State>, | ||
draft: Draft<State>, | ||
...extraArgs: Args | ||
) => void | Result | ||
): (base: State, ...extraArgs: Args) => void extends Result ? State : Result | ||
this: Draft<Base>, | ||
draft: Draft<Base>, | ||
...rest: Rest | ||
) => Return, | ||
defaultBase?: Default | ||
): (base: Base | undefined, ...rest: Rest) => Produced<Base, Return> | ||
} | ||
@@ -104,6 +108,12 @@ | ||
/** Use a class type for `nothing` so its type is unique */ | ||
declare class Nothing { | ||
// This lets us do `Exclude<T, Nothing>` | ||
private _: any | ||
} | ||
/** | ||
* The sentinel value returned by producers to replace the draft with undefined. | ||
*/ | ||
export const nothing: undefined | ||
export const nothing: Nothing | ||
@@ -110,0 +120,0 @@ /** |
{ | ||
"name": "immer", | ||
"version": "1.9.2", | ||
"version": "1.9.3", | ||
"description": "Create your next immutable state by mutating the current one", | ||
@@ -18,3 +18,5 @@ "main": "dist/immer.js", | ||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage", | ||
"build": "rimraf dist/ && cross-env NODE_ENV=production rollup -c && cpx 'src/immer.{d.ts,js.flow}' dist -v" | ||
"build": "rimraf dist/ && yarn-or-npm rollup && yarn-or-npm typed", | ||
"rollup": "cross-env NODE_ENV=production rollup -c", | ||
"typed": "cpx 'src/immer.{d.ts,js.flow}' dist -v" | ||
}, | ||
@@ -21,0 +23,0 @@ "husky": { |
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
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
244827
1687