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 fileTake it
The walk stopped after 200 commits: lines older than 8571fe0 point at it.
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1why, one step back1import {NOTHING} from "../internal"docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 2–10why, one step back23type 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 back11 * If the lib "ES2015.Collection" is not included in tsconfig.json,docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 12why, one step back12 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)Fix typo (#985)ddronebefore adoptionba0a79a
line 13why, one step back13 * or `{}` (from the node types), in both cases entering an infinite recursion indocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 14–18why, one step back14 * 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 anychore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
line 19why, one step back19 true | false extends (T extends never ? true : false)docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 20–21why, one step back20 ? Fallback // fallback if empty type21 : keyof T extends neverchore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 22–23why, one step back22 ? Fallback // original type23 : Tdocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 24–30why, one step back2425/**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>>30fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
lines 31–32why, one step back31export 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 back33 ? IsPlainArray<T> extends true34 ? Draft<T[number]>[]35 : WritableNonArrayDraft<T>fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
lines 36–38why, one step back36 : WritableNonArrayDraft<T>37 : WritableNonArrayDraft<T>38fix: improve higher order type inference for WritableDraft (#1205)Homa Wongbefore adoption7390d6c
lines 39–49why, one step back39/**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 : false49fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
line 50why, one step back50type WritableNonArrayDraft<T> = {fix: improve higher order type inference for WritableDraft (#1205)Homa Wongbefore adoption7390d6c
line 51why, one step back51 -readonly [K in keyof T]: {_: T[K]} extends {_: infer V}fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
lines 52–56why, one step back52 ? V extends object53 ? Draft<V>54 : V55 : never56}docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 57why, one step back57chore: Add note explaining Draft type (#967)Stephen Michelbefore adoption54ecc9b
line 58why, one step back58/** Convert a readonly type into a mutable type, if possible */docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 59–61why, one step back59export type Draft<T> = T extends PrimitiveType60 ? T61 : T extends AtomicObjectchore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 62–71why, one step back62 ? T63 : 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 : Tdocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 72–76why, one step back7273/** Convert a mutable type into a readonly type */74export type Immutable<T> = T extends PrimitiveType75 ? T76 : T extends AtomicObjectchore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 77–86why, one step back77 ? T78 : 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 : Tdocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 87–96why, one step back8788export 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 back97type FromNothing<T> = T extends typeof NOTHING ? undefined : Tdocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 98–112why, one step back9899/** 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 | undefinedMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 113why, one step back113 | (State extends undefined ? typeof NOTHING : never)docs: Update performance.mdx (#942)anabefore adoption8571fe0
line 114why, one step back114Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 115why, one step back115type ReturnTypeWithPatchesIfNeeded<docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 116–117why, one step back116 State,117 UsePatches extends booleanMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 118why, one step back118> = UsePatches extends true ? PatchesTuple<State> : Statedocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 119–130why, one step back119120/**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: Argschore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
line 131why, one step back131 ) => ValidRecipeReturnType<Draft<State>>docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 132–145why, one step back132 : never133 : 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 typeMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 146why, one step back146 ? ReturnType<Recipe> extends ValidRecipeReturnType<DraftState>docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 147–149why, one step back147 ? (148 base: Immutable<DraftState>,149 ...args: RestArgschore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
line 150why, one step back150 ) => 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 eitherdocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 151–160why, one step back151 : never // incorrect return type152 : never // not a function153154type InferCurriedFromInitialStateAndRecipe<155 State,156 Recipe,157 UsePatches extends boolean158> = Recipe extends (159 draft: Draft<State>,160 ...rest: infer RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 161why, one step back161) => ValidRecipeReturnType<State>docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 162–164why, one step back162 ? (163 base?: State | undefined,164 ...args: RestArgschore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
line 165why, one step back165 ) => ReturnTypeWithPatchesIfNeeded<State, UsePatches>docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 166–194why, one step back166 : never // recipe doesn't match initial state167168/**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 back195 <Recipe extends AnyFunc>(196 recipe: Recipe197 ): InferCurriedFromRecipe<Recipe, false>docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 198–206why, one step back198199 /** 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) => Statechore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 207–209why, one step back207 <State>(208 recipe: (state: Draft<State>) => ValidRecipeReturnType<State>209 ): (state: State) => Statedocs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 210–236why, one step back210 <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 back237238 /** 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>252docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 253–263why, one step back253 <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}263feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787
lines 264–266why, one step back264/**265 * The type for `recipe function`266 */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 267why, one step back267export 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 back268docs: Update performance.mdx (#942)anabefore adoption8571fe0
lines 269–271why, one step back269// 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() {}