Skip to content
Myr

types-external.ts

This file

Path

src/types/types-external.ts

239 lines · 7.4 KiB · TypeScript

Last changed by

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

History of this file

Take it

The walk stopped after 200 commits: lines older than 6555173 point at it.

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 1why, one step back
1import {NOTHING} from "../internal"

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 2–10why, one step back
2
3type AnyFunc = (...args: any[]) => any45type PrimitiveType = number | string | boolean67/** Object types that should never be mapped */8type AtomicObject = Function | Promise<any> | Date | RegExp910/**

Fix typo (#985)ddronebefore adoptionba0a79a

line 11why, one step back
11 * If the lib "ES2015.Collection" is not included in tsconfig.json,

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 12why, one step back
12 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)

Fix typo (#985)ddronebefore adoptionba0a79a

line 13why, one step back
13 * or `{}` (from the node types), in both cases entering an infinite recursion in

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 14–32why, one step back
14 * pattern matching type mappings
15 * This type can be used to cast these types to `void` in these cases.16 */17export type IfAvailable<T, Fallback = void> =18 // fallback if any19 true | false extends (T extends never20 ? true21 : false)22 ? Fallback // fallback if empty type23 : keyof T extends never24 ? Fallback // original type25 : T2627/**28 * These should also never be mapped but must be tested after regular Map and29 * Set30 */31type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>32

fix: revert earlier fix (#990) for recursive types (#1014)Nathan Bieremabefore adoption3eeb331

line 33why, one step back
33export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 34why, one step back
34

chore: Add note explaining Draft type (#967)Stephen Michelbefore adoption54ecc9b

line 35why, one step back
35/** Convert a readonly type into a mutable type, if possible */

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 36–39why, one step back
36export type Draft<T> = T extends PrimitiveType
37 ? T38 : T extends AtomicObject39 ? T

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 40why, one step back
40 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 41why, one step back
41 ? Map<Draft<K>, Draft<V>>

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 42why, one step back
42 : T extends ReadonlySet<infer V> // Set extends ReadonlySet

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 43–54why, one step back
43 ? Set<Draft<V>>
44 : T extends WeakReferences45 ? T46 : T extends object47 ? WritableDraft<T>48 : T4950/** Convert a mutable type into a readonly type */51export type Immutable<T> = T extends PrimitiveType52 ? T53 : T extends AtomicObject54 ? T

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 55why, one step back
55 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 56why, one step back
56 ? ReadonlyMap<Immutable<K>, Immutable<V>>

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 57why, one step back
57 : T extends ReadonlySet<infer V> // Set extends ReadonlySet

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 58–73why, one step back
58 ? ReadonlySet<Immutable<V>>
59 : T extends WeakReferences60 ? T61 : T extends object62 ? {readonly [K in keyof T]: Immutable<T[K]>}63 : T6465export interface Patch {66 op: "replace" | "remove" | "add"67 path: (string | number)[]68 value?: any69}7071export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7273/** Converts `nothing` into `undefined` */

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 74why, one step back
74type FromNothing<T> = T extends typeof NOTHING ? undefined : T

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 75–89why, one step back
75
76/** The inferred return type of `produce` */77export type Produced<Base, Return> = Return extends void78 ? Base79 : FromNothing<Return>8081/**82 * Utility types83 */84type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8586type ValidRecipeReturnType<State> =87 | State88 | void89 | undefined

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 90why, one step back
90 | (State extends undefined ? typeof NOTHING : never)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 91why, one step back
91

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 92why, one step back
92type ReturnTypeWithPatchesIfNeeded<

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 93–94why, one step back
93 State,
94 UsePatches extends boolean

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 95why, one step back
95> = UsePatches extends true ? PatchesTuple<State> : State

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 96–122why, one step back
96
97/**98 * Core Producer inference99 */100type InferRecipeFromCurried<Curried> = Curried extends (101 base: infer State,102 ...rest: infer Args103) => any // extra assertion to make sure this is a proper curried function (state, args) => state104 ? ReturnType<Curried> extends State105 ? (106 draft: Draft<State>,107 ...rest: Args108 ) => ValidRecipeReturnType<Draft<State>>109 : never110 : never111112type InferInitialStateFromCurried<Curried> = Curried extends (113 base: infer State,114 ...rest: any[]115) => any // extra assertion to make sure this is a proper curried function (state, args) => state116 ? State117 : never118119type InferCurriedFromRecipe<120 Recipe,121 UsePatches extends boolean122> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 123why, one step back
123 ? ReturnType<Recipe> extends ValidRecipeReturnType<DraftState>

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 124–126why, one step back
124 ? (
125 base: Immutable<DraftState>,126 ...args: RestArgs

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 127why, one step back
127 ) => ReturnTypeWithPatchesIfNeeded<DraftState, UsePatches> // N.b. we return mutable draftstate, in case the recipe's first arg isn't read only, and that isn't expected as output either

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 128–137why, one step back
128 : never // incorrect return type
129 : never // not a function130131type InferCurriedFromInitialStateAndRecipe<132 State,133 Recipe,134 UsePatches extends boolean135> = Recipe extends (136 draft: Draft<State>,137 ...rest: infer RestArgs

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 138why, one step back
138) => ValidRecipeReturnType<State>

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 139–141why, one step back
139 ? (
140 base?: State | undefined,141 ...args: RestArgs

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 142why, one step back
142 ) => ReturnTypeWithPatchesIfNeeded<State, UsePatches>

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 143–226why, one step back
143 : never // recipe doesn't match initial state
144145/**146 * The `produce` function takes a value and a "recipe function" (whose147 * return value often depends on the base state). The recipe function is148 * free to mutate its first argument however it wants. All mutations are149 * only ever applied to a __copy__ of the base state.150 *151 * Pass only a function to create a "curried producer" which relieves you152 * from passing the recipe function every time.153 *154 * Only plain objects and arrays are made mutable. All other objects are155 * considered uncopyable.156 *157 * Note: This function is __bound__ to its `Immer` instance.158 *159 * @param {any} base - the initial state160 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified161 * @param {Function} patchListener - optional function that will be called with all the patches produced here162 * @returns {any} a new state, or the initial state if nothing was modified163 */164export interface IProduce {165 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */166 <Curried>(167 recipe: InferRecipeFromCurried<Curried>,168 initialState?: InferInitialStateFromCurried<Curried>169 ): Curried170171 /** Curried producer that infers curried from the recipe */172 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<173 Recipe,174 false175 >176177 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */178 <State>(179 recipe: (180 state: Draft<State>,181 initialState: State182 ) => ValidRecipeReturnType<State>183 ): (state?: State) => State184 <State, Args extends any[]>(185 recipe: (186 state: Draft<State>,187 ...args: Args188 ) => ValidRecipeReturnType<State>,189 initialState: State190 ): (state?: State, ...args: Args) => State191 <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (192 state: State193 ) => State194 <State, Args extends any[]>(195 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>196 ): (state: State, ...args: Args) => State197198 /** Curried producer with initial state, infers recipe from initial state */199 <State, Recipe extends Function>(200 recipe: Recipe,201 initialState: State202 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>203204 /** Normal producer */205 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.206 base: Base,207 recipe: (draft: D) => ValidRecipeReturnType<D>,208 listener?: PatchListener209 ): Base210}211212/**213 * Like `produce`, but instead of just returning the new state,214 * a tuple is returned with [nextState, patches, inversePatches]215 *216 * Like produce, this function supports currying217 */218export interface IProduceWithPatches {219 // Types copied from IProduce, wrapped with PatchesTuple220 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>221 <State, Recipe extends Function>(222 recipe: Recipe,223 initialState: State224 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>225 <Base, D = Draft<Base>>(226 base: Base,

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

line 227why, one step back
227 recipe: (draft: D) => ValidRecipeReturnType<D>,

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 228why, one step back
228 listener?: PatchListener

fix: produceWithPatches should not wrap result in Immutable, fixes #850, #881Michel Weststratebefore adoptiond8f2636

line 229why, one step back
229 ): PatchesTuple<Base>

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 230–231why, one step back
230}
231

feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787

lines 232–234why, one step back
232/**
233 * The type for `recipe function`234 */

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 235why, one step back
235export type Producer<T> = (draft: Draft<T>) => ValidRecipeReturnType<Draft<T>>

feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787

line 236why, one step back
236

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 237–239why, one step back
237// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..
238// hopefully it get's tree-shaken away for everyone :)239export function never_used() {}