Skip to content
Myr

types-external.ts

This file

Path

src/types/types-external.ts

271 lines · 8.5 KiB · TypeScript

Last changed by

fix: correct curried produceWithPatches return type (#1249)Alexander Kireyevbefore adoptiona3be9df

History of this file

Take it

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

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 2–18why, 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/**11 * If the lib "ES2015.Collection" 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 infinite 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 any

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 19why, one step back
19 true | false extends (T extends never ? true : false)

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 20–21why, one step back
20 ? Fallback // fallback if empty type
21 : keyof T extends never

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 22–23why, one step back
22 ? Fallback // original type
23 : T

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 24–30why, one step back
24
25/**26 * These should also never be mapped but must be tested after regular Map and27 * Set28 */29type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>30

fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694

lines 31–32why, one step back
31export type WritableDraft<T> = T extends any[]
32 ? number extends T["length"]

fix: improve higher order type inference for WritableDraft (#1205)Homa Wongbefore adoption7390d6c

lines 33–35why, one step back
33 ? IsPlainArray<T> extends true
34 ? Draft<T[number]>[]35 : WritableNonArrayDraft<T>

fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694

lines 36–38why, one step back
36 : WritableNonArrayDraft<T>
37 : WritableNonArrayDraft<T>38

fix: improve higher order type inference for WritableDraft (#1205)Homa Wongbefore adoption7390d6c

lines 39–49why, one step back
39/**
40 * Distinguishes plain array types (`number[]`, `readonly string[]`, etc.) from41 * tuple types — including variadic tuples like `[boolean, ...number[]]` — whose42 * `length` is also `number` but must not be widened to `Element[]`.43 */44type IsPlainArray<T extends readonly any[]> = T extends readonly (infer U)[]45 ? U[] extends T46 ? true47 : false48 : false49

fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694

line 50why, one step back
50type WritableNonArrayDraft<T> = {

fix: improve higher order type inference for WritableDraft (#1205)Homa Wongbefore adoption7390d6c

line 51why, one step back
51 -readonly [K in keyof T]: {_: T[K]} extends {_: infer V}

fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694

lines 52–56why, one step back
52 ? V extends object
53 ? Draft<V>54 : V55 : never56}

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 57–61why, one step back
57
58/** Convert a readonly type into a mutable type, if possible */59export type Draft<T> = T extends PrimitiveType60 ? T61 : T extends AtomicObject

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 62–71why, one step back
62 ? T
63 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap64 ? Map<Draft<K>, Draft<V>>65 : T extends ReadonlySet<infer V> // Set extends ReadonlySet66 ? Set<Draft<V>>67 : T extends WeakReferences68 ? T69 : T extends object70 ? WritableDraft<T>71 : T

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 72–76why, one step back
72
73/** Convert a mutable type into a readonly type */74export type Immutable<T> = T extends PrimitiveType75 ? T76 : T extends AtomicObject

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 77–86why, one step back
77 ? T
78 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap79 ? ReadonlyMap<Immutable<K>, Immutable<V>>80 : T extends ReadonlySet<infer V> // Set extends ReadonlySet81 ? ReadonlySet<Immutable<V>>82 : T extends WeakReferences83 ? T84 : T extends object85 ? {readonly [K in keyof T]: Immutable<T[K]>}86 : T

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 87–96why, one step back
87
88export interface Patch {89 op: "replace" | "remove" | "add"90 path: (string | number)[]91 value?: any92}9394export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void9596/** Converts `nothing` into `undefined` */

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 98–112why, one step back
98
99/** The inferred return type of `produce` */100export type Produced<Base, Return> = Return extends void101 ? Base102 : FromNothing<Return>103104/**105 * Utility types106 */107type PatchesTuple<T> = readonly [T, Patch[], Patch[]]108109type ValidRecipeReturnType<State> =110 | State111 | void112 | undefined

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

line 114why, one step back
114

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

line 115why, one step back
115type ReturnTypeWithPatchesIfNeeded<

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 116–117why, one step back
116 State,
117 UsePatches extends boolean

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 119–130why, one step back
119
120/**121 * Core Producer inference122 */123type InferRecipeFromCurried<Curried> = Curried extends (124 base: infer State,125 ...rest: infer Args126) => any // extra assertion to make sure this is a proper curried function (state, args) => state127 ? ReturnType<Curried> extends State128 ? (129 draft: Draft<State>,130 ...rest: Args

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 132–145why, one step back
132 : never
133 : never134135type InferInitialStateFromCurried<Curried> = Curried extends (136 base: infer State,137 ...rest: any[]138) => any // extra assertion to make sure this is a proper curried function (state, args) => state139 ? State140 : never141142type InferCurriedFromRecipe<143 Recipe,144 UsePatches extends boolean145> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 147–149why, one step back
147 ? (
148 base: Immutable<DraftState>,149 ...args: RestArgs

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 150why, one step back
150 ) => 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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 151–160why, one step back
151 : never // incorrect return type
152 : never // not a function153154type InferCurriedFromInitialStateAndRecipe<155 State,156 Recipe,157 UsePatches extends boolean158> = Recipe extends (159 draft: Draft<State>,160 ...rest: infer RestArgs

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

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 162–164why, one step back
162 ? (
163 base?: State | undefined,164 ...args: RestArgs

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 166–194why, one step back
166 : never // recipe doesn't match initial state
167168/**169 * The `produce` function takes a value and a "recipe function" (whose170 * return value often depends on the base state). The recipe function is171 * free to mutate its first argument however it wants. All mutations are172 * only ever applied to a __copy__ of the base state.173 *174 * Pass only a function to create a "curried producer" which relieves you175 * from passing the recipe function every time.176 *177 * Only plain objects and arrays are made mutable. All other objects are178 * considered uncopyable.179 *180 * Note: This function is __bound__ to its `Immer` instance.181 *182 * @param {any} base - the initial state183 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified184 * @param {Function} patchListener - optional function that will be called with all the patches produced here185 * @returns {any} a new state, or the initial state if nothing was modified186 */187export interface IProduce {188 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */189 <Curried>(190 recipe: InferRecipeFromCurried<Curried>,191 initialState?: InferInitialStateFromCurried<Curried>192 ): Curried193194 /** Curried producer that infers curried from the recipe */

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 195–197why, one step back
195 <Recipe extends AnyFunc>(
196 recipe: Recipe197 ): InferCurriedFromRecipe<Recipe, false>

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 198–206why, one step back
198
199 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */200 <State, Args extends any[]>(201 recipe: (202 state: Draft<State>,203 ...args: Args204 ) => ValidRecipeReturnType<State>,205 initialState: State206 ): (state?: State, ...args: Args) => State

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 207–209why, one step back
207 <State>(
208 recipe: (state: Draft<State>) => ValidRecipeReturnType<State>209 ): (state: State) => State

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 210–236why, one step back
210 <State, Args extends any[]>(
211 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>212 ): (state: State, ...args: Args) => State213214 /** Curried producer with initial state, infers recipe from initial state */215 <State, Recipe extends Function>(216 recipe: Recipe,217 initialState: State218 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>219220 /** Normal producer */221 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.222 base: Base,223 recipe: (draft: D) => ValidRecipeReturnType<D>,224 listener?: PatchListener225 ): Base226}227228/**229 * Like `produce`, but instead of just returning the new state,230 * a tuple is returned with [nextState, patches, inversePatches]231 *232 * Like produce, this function supports currying233 */234export interface IProduceWithPatches {235 // Types copied from IProduce, wrapped with PatchesTuple236 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>

fix: correct curried produceWithPatches return type (#1249)Alexander Kireyevbefore adoptiona3be9df

lines 237–252why, one step back
237
238 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */239 <State, Args extends any[]>(240 recipe: (241 state: Draft<State>,242 ...args: Args243 ) => ValidRecipeReturnType<State>,244 initialState: State245 ): (state?: State, ...args: Args) => PatchesTuple<State>246 <State>(247 recipe: (state: Draft<State>) => ValidRecipeReturnType<State>248 ): (state: State) => PatchesTuple<State>249 <State, Args extends any[]>(250 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>251 ): (state: State, ...args: Args) => PatchesTuple<State>252

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 253–263why, one step back
253 <State, Recipe extends Function>(
254 recipe: Recipe,255 initialState: State256 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>257 <Base, D = Draft<Base>>(258 base: Base,259 recipe: (draft: D) => ValidRecipeReturnType<D>,260 listener?: PatchListener261 ): PatchesTuple<Base>262}263

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

lines 264–266why, one step back
264/**
265 * The type for `recipe function`266 */

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

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

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

line 268why, one step back
268

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

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