types-external.ts
This file
Path
src/types/types-external.ts
251 lines · 7.6 KiB · TypeScript
Last changed by
fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
History of this fileTake it
The walk stopped after 200 commits: lines older than 3f9a94e point at it.
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1why, one step back1import {NOTHING} from "../internal"chore: let's test before publishMichel Weststratebefore adoption3f9a94e
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,chore: let's test before publishMichel Weststratebefore adoption3f9a94e
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 inchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 14–32why, 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 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>>32fix: recursive T for WritableDraft (#1197)Homa Wongbefore adoption78ea694
lines 33–45why, one step back33export type WritableDraft<T> = T extends any[]34 ? number extends T["length"]35 ? Draft<T[number]>[]36 : WritableNonArrayDraft<T>37 : WritableNonArrayDraft<T>3839type WritableNonArrayDraft<T> = {40 -readonly [K in keyof T]: T[K] extends infer V41 ? V extends object42 ? Draft<V>43 : V44 : never45}chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 46why, one step back46chore: Add note explaining Draft type (#967)Stephen Michelbefore adoption54ecc9b
line 47why, one step back47/** Convert a readonly type into a mutable type, if possible */chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 48–51why, one step back48export type Draft<T> = T extends PrimitiveType49 ? T50 : T extends AtomicObject51 ? TMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 52why, one step back52 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMapchore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 53why, one step back53 ? Map<Draft<K>, Draft<V>>Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 54why, one step back54 : T extends ReadonlySet<infer V> // Set extends ReadonlySetchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 55–66why, one step back55 ? Set<Draft<V>>56 : T extends WeakReferences57 ? T58 : T extends object59 ? WritableDraft<T>60 : T6162/** Convert a mutable type into a readonly type */63export type Immutable<T> = T extends PrimitiveType64 ? T65 : T extends AtomicObject66 ? TMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 67why, one step back67 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMapchore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 68why, one step back68 ? ReadonlyMap<Immutable<K>, Immutable<V>>Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 69why, one step back69 : T extends ReadonlySet<infer V> // Set extends ReadonlySetchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 70–85why, one step back70 ? ReadonlySet<Immutable<V>>71 : T extends WeakReferences72 ? T73 : T extends object74 ? {readonly [K in keyof T]: Immutable<T[K]>}75 : T7677export interface Patch {78 op: "replace" | "remove" | "add"79 path: (string | number)[]80 value?: any81}8283export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void8485/** Converts `nothing` into `undefined` */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 86why, one step back86type FromNothing<T> = T extends typeof NOTHING ? undefined : Tchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 87–101why, one step back8788/** The inferred return type of `produce` */89export type Produced<Base, Return> = Return extends void90 ? Base91 : FromNothing<Return>9293/**94 * Utility types95 */96type PatchesTuple<T> = readonly [T, Patch[], Patch[]]9798type ValidRecipeReturnType<State> =99 | State100 | void101 | undefinedMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 102why, one step back102 | (State extends undefined ? typeof NOTHING : never)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 103why, one step back103Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 104why, one step back104type ReturnTypeWithPatchesIfNeeded<chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 105–106why, one step back105 State,106 UsePatches extends booleanMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 107why, one step back107> = UsePatches extends true ? PatchesTuple<State> : Statechore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 108–134why, one step back108109/**110 * Core Producer inference111 */112type InferRecipeFromCurried<Curried> = Curried extends (113 base: infer State,114 ...rest: infer Args115) => any // extra assertion to make sure this is a proper curried function (state, args) => state116 ? ReturnType<Curried> extends State117 ? (118 draft: Draft<State>,119 ...rest: Args120 ) => ValidRecipeReturnType<Draft<State>>121 : never122 : never123124type InferInitialStateFromCurried<Curried> = Curried extends (125 base: infer State,126 ...rest: any[]127) => any // extra assertion to make sure this is a proper curried function (state, args) => state128 ? State129 : never130131type InferCurriedFromRecipe<132 Recipe,133 UsePatches extends boolean134> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return typeMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 135why, one step back135 ? ReturnType<Recipe> extends ValidRecipeReturnType<DraftState>chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 136–138why, one step back136 ? (137 base: Immutable<DraftState>,138 ...args: RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 139why, one step back139 ) => 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 eitherchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 140–149why, one step back140 : never // incorrect return type141 : never // not a function142143type InferCurriedFromInitialStateAndRecipe<144 State,145 Recipe,146 UsePatches extends boolean147> = Recipe extends (148 draft: Draft<State>,149 ...rest: infer RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 150why, one step back150) => ValidRecipeReturnType<State>chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 151–153why, one step back151 ? (152 base?: State | undefined,153 ...args: RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 154why, one step back154 ) => ReturnTypeWithPatchesIfNeeded<State, UsePatches>chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 155–238why, one step back155 : never // recipe doesn't match initial state156157/**158 * The `produce` function takes a value and a "recipe function" (whose159 * return value often depends on the base state). The recipe function is160 * free to mutate its first argument however it wants. All mutations are161 * only ever applied to a __copy__ of the base state.162 *163 * Pass only a function to create a "curried producer" which relieves you164 * from passing the recipe function every time.165 *166 * Only plain objects and arrays are made mutable. All other objects are167 * considered uncopyable.168 *169 * Note: This function is __bound__ to its `Immer` instance.170 *171 * @param {any} base - the initial state172 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified173 * @param {Function} patchListener - optional function that will be called with all the patches produced here174 * @returns {any} a new state, or the initial state if nothing was modified175 */176export interface IProduce {177 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */178 <Curried>(179 recipe: InferRecipeFromCurried<Curried>,180 initialState?: InferInitialStateFromCurried<Curried>181 ): Curried182183 /** Curried producer that infers curried from the recipe */184 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<185 Recipe,186 false187 >188189 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */190 <State>(191 recipe: (192 state: Draft<State>,193 initialState: State194 ) => ValidRecipeReturnType<State>195 ): (state?: State) => State196 <State, Args extends any[]>(197 recipe: (198 state: Draft<State>,199 ...args: Args200 ) => ValidRecipeReturnType<State>,201 initialState: State202 ): (state?: State, ...args: Args) => State203 <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (204 state: State205 ) => State206 <State, Args extends any[]>(207 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>208 ): (state: State, ...args: Args) => State209210 /** Curried producer with initial state, infers recipe from initial state */211 <State, Recipe extends Function>(212 recipe: Recipe,213 initialState: State214 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>215216 /** Normal producer */217 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.218 base: Base,219 recipe: (draft: D) => ValidRecipeReturnType<D>,220 listener?: PatchListener221 ): Base222}223224/**225 * Like `produce`, but instead of just returning the new state,226 * a tuple is returned with [nextState, patches, inversePatches]227 *228 * Like produce, this function supports currying229 */230export interface IProduceWithPatches {231 // Types copied from IProduce, wrapped with PatchesTuple232 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>233 <State, Recipe extends Function>(234 recipe: Recipe,235 initialState: State236 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>237 <Base, D = Draft<Base>>(238 base: Base,fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d
line 239why, one step back239 recipe: (draft: D) => ValidRecipeReturnType<D>,chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 240why, one step back240 listener?: PatchListenerfix: produceWithPatches should not wrap result in Immutable, fixes #850, #881Michel Weststratebefore adoptiond8f2636
line 241why, one step back241 ): PatchesTuple<Base>chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 242–243why, one step back242}243feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787
lines 244–246why, one step back244/**245 * The type for `recipe function`246 */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 247why, one step back247export type Producer<T> = (draft: Draft<T>) => ValidRecipeReturnType<Draft<T>>feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787
line 248why, one step back248chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 249–251why, one step back249// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..250// hopefully it get's tree-shaken away for everyone :)251export function never_used() {}