Skip to content
Myr

types-external.ts

This file

Path

src/types/types-external.ts

256 lines · 8.0 KiB · TypeScript

Last changed by

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

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–2why, one step back
1import {Nothing} from "../internal"
2

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 3why, one step back
3type AnyFunc = (...args: any[]) => any

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 4why, one step back
4

fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe

lines 5–6why, one step back
5type PrimitiveType = number | string | boolean
6

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 7why, one step back
7/** Object types that should never be mapped */

fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe

line 8why, one step back
8type AtomicObject = Function | Promise<any> | Date | RegExp

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 9–10why, one step back
9
10/**

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

lines 11–27why, one step back
11 * If the lib "ES2105.collections" is not included in tsconfig.json,
12 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)13 * or `{}` (from the node types), in both cases entering an infite recursion in14 * pattern matching type mappings15 * 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/**

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 28–30why, one step back
28 * These should also never be mapped but must be tested after regular Map and
29 * Set30 */

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

line 31why, one step back
31type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 32why, one step back
32

fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0

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

fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe

line 35why, one step back
35export type Draft<T> = T extends PrimitiveType

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 36why, one step back
36 ? T

fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe

lines 37–38why, one step back
37 : T extends AtomicObject
38 ? T

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 42–45why, one step back
42 ? Set<Draft<V>>
43 : T extends WeakReferences44 ? T45 : T extends object

fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0

line 46why, one step back
46 ? WritableDraft<T>

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 47–49why, one step back
47 : T
4849/** Convert a mutable type into a readonly type */

fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe

lines 50–52why, one step back
50export type Immutable<T> = T extends PrimitiveType
51 ? T52 : T extends AtomicObject

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 53why, one step back
53 ? T

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 57–82why, one step back
57 ? ReadonlySet<Immutable<V>>
58 : T extends WeakReferences59 ? T60 : T extends object61 ? {readonly [K in keyof T]: Immutable<T[K]>}62 : T6364export interface Patch {65 op: "replace" | "remove" | "add"66 path: (string | number)[]67 value?: any68}6970export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7172/** Converts `nothing` into `undefined` */73type FromNothing<T> = T extends Nothing ? undefined : T7475/** The inferred return type of `produce` */76export type Produced<Base, Return> = Return extends void77 ? Base78 : Return extends Promise<infer Result>79 ? Promise<Result extends void ? Base : FromNothing<Result>>80 : FromNothing<Return>8182/**

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 83–155why, one step back
83 * Utility types
84 */85type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8687type ValidRecipeReturnType<State> =88 | State89 | void90 | undefined91 | (State extends undefined ? Nothing : never)9293type ValidRecipeReturnTypePossiblyPromise<State> =94 | ValidRecipeReturnType<State>95 | Promise<ValidRecipeReturnType<State>>9697type PromisifyReturnIfNeeded<98 State,99 Recipe extends AnyFunc,100 UsePatches extends boolean101> = ReturnType<Recipe> extends Promise<any>102 ? Promise<UsePatches extends true ? PatchesTuple<State> : State>103 : UsePatches extends true104 ? PatchesTuple<State>105 : State106107/**108 * Core Producer inference109 */110type InferRecipeFromCurried<Curried> = Curried extends (111 base: infer State,112 ...rest: infer Args113) => any // extra assertion to make sure this is a proper curried function (state, args) => state114 ? ReturnType<Curried> extends State115 ? (116 draft: Draft<State>,117 ...rest: Args118 ) => ValidRecipeReturnType<Draft<State>>119 : never120 : never121122type InferInitialStateFromCurried<Curried> = Curried extends (123 base: infer State,124 ...rest: any[]125) => any // extra assertion to make sure this is a proper curried function (state, args) => state126 ? State127 : never128129type InferCurriedFromRecipe<130 Recipe,131 UsePatches extends boolean132> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type133 ? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState>134 ? (135 base: Immutable<DraftState>,136 ...args: RestArgs137 ) => PromisifyReturnIfNeeded<DraftState, Recipe, 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 either138 : never // incorrect return type139 : never // not a function140141type InferCurriedFromInitialStateAndRecipe<142 State,143 Recipe,144 UsePatches extends boolean145> = Recipe extends (146 draft: Draft<State>,147 ...rest: infer RestArgs148) => ValidRecipeReturnTypePossiblyPromise<State>149 ? (150 base?: State | undefined,151 ...args: RestArgs152 ) => PromisifyReturnIfNeeded<State, Recipe, UsePatches>153 : never // recipe doesn't match initial state154155/**

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 156–174why, one step back
156 * The `produce` function takes a value and a "recipe function" (whose
157 * return value often depends on the base state). The recipe function is158 * free to mutate its first argument however it wants. All mutations are159 * only ever applied to a __copy__ of the base state.160 *161 * Pass only a function to create a "curried producer" which relieves you162 * from passing the recipe function every time.163 *164 * Only plain objects and arrays are made mutable. All other objects are165 * considered uncopyable.166 *167 * Note: This function is __bound__ to its `Immer` instance.168 *169 * @param {any} base - the initial state170 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified171 * @param {Function} patchListener - optional function that will be called with all the patches produced here172 * @returns {any} a new state, or the initial state if nothing was modified173 */174export interface IProduce {

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 175–179why, one step back
175 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */
176 <Curried>(177 recipe: InferRecipeFromCurried<Curried>,178 initialState?: InferInitialStateFromCurried<Curried>179 ): Curried

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 180why, one step back
180

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 181–209why, one step back
181 /** Curried producer that infers curried from the recipe */
182 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<183 Recipe,184 false185 >186187 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */188 <State>(189 recipe: (190 state: Draft<State>,191 initialState: State192 ) => ValidRecipeReturnType<State>193 ): (state?: State) => State194 <State, Args extends any[]>(195 recipe: (196 state: Draft<State>,197 ...args: Args198 ) => ValidRecipeReturnType<State>,199 initialState: State200 ): (state?: State, ...args: Args) => State201 <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (202 state: State203 ) => State204 <State, Args extends any[]>(205 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>206 ): (state: State, ...args: Args) => State207208 /** Curried producer with initial state, infers recipe from initial state */209 <State, Recipe extends Function>(

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 210why, one step back
210 recipe: Recipe,

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 211–212why, one step back
211 initialState: State
212 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 213–214why, one step back
213
214 /** Normal producer */

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 215why, one step back
215 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 216why, one step back
216 base: Base,

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 219why, one step back
219 ): Base

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

lines 220–226why, one step back
220
221 /** Promisified dormal producer */222 <Base, D = Draft<Base>>(223 base: Base,224 recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,225 listener?: PatchListener226 ): Promise<Base>

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 227–235why, one step back
227}
228229/**230 * Like `produce`, but instead of just returning the new state,231 * a tuple is returned with [nextState, patches, inversePatches]232 *233 * Like produce, this function supports currying234 */235export interface IProduceWithPatches {

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 236–238why, one step back
236 // Types copied from IProduce, wrapped with PatchesTuple
237 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>238 <State, Recipe extends Function>(

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 239why, one step back
239 recipe: Recipe,

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 240–242why, one step back
240 initialState: State
241 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>242 <Base, D = Draft<Base>>(

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 243why, one step back
243 base: Base,

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 244–245why, one step back
244 recipe: (draft: D) => ValidRecipeReturnType<Base>,
245 listener?: PatchListener

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 247–250why, one step back
247 <Base, D = Draft<Base>>(
248 base: Base,249 recipe: (draft: D) => Promise<ValidRecipeReturnType<Base>>,250 listener?: PatchListener

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

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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