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 fileTake it
The walk stopped after 200 commits: lines older than b7fcf7f point at it.
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1why, one step back1import {NOTHING} from "../internal"docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 2why, one step back2feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 3why, one step back3type AnyFunc = (...args: any[]) => anydocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 4–10why, one step back45type 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: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
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: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
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: revert earlier fix (#990) for recursive types (#1014)Nathan Bieremabefore adoption3eeb331
line 33why, one step back33export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 34why, one step back34chore: Add note explaining Draft type (#967)Stephen Michelbefore adoption54ecc9b
line 35why, one step back35/** Convert a readonly type into a mutable type, if possible */docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 36–39why, one step back36export type Draft<T> = T extends PrimitiveType37 ? T38 : T extends AtomicObject39 ? TMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 40why, one step back40 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMapdocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 41why, one step back41 ? Map<Draft<K>, Draft<V>>Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 42why, one step back42 : T extends ReadonlySet<infer V> // Set extends ReadonlySetdocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 43–54why, one step back43 ? 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 ? TMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 55why, one step back55 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMapdocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 56why, one step back56 ? ReadonlyMap<Immutable<K>, Immutable<V>>Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 57why, one step back57 : T extends ReadonlySet<infer V> // Set extends ReadonlySetdocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 58–73why, one step back58 ? 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 back74type FromNothing<T> = T extends typeof NOTHING ? undefined : Tdocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 75–81why, one step back7576/** The inferred return type of `produce` */77export type Produced<Base, Return> = Return extends void78 ? Base79 : FromNothing<Return>8081/**feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 82–89why, one step back82 * Utility types83 */84type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8586type ValidRecipeReturnType<State> =87 | State88 | void89 | undefinedMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 90why, one step back90 | (State extends undefined ? typeof NOTHING : never)feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 91why, one step back91Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 92why, one step back92type ReturnTypeWithPatchesIfNeeded<feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 93–94why, one step back93 State,94 UsePatches extends booleanMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 95why, one step back95> = UsePatches extends true ? PatchesTuple<State> : Statefeature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 96–122why, one step back9697/**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 typeMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 123why, one step back123 ? ReturnType<Recipe> extends ValidRecipeReturnType<DraftState>feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 124–126why, one step back124 ? (125 base: Immutable<DraftState>,126 ...args: RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 127why, one step back127 ) => 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 eitherfeature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 128–137why, one step back128 : never // incorrect return type129 : never // not a function130131type InferCurriedFromInitialStateAndRecipe<132 State,133 Recipe,134 UsePatches extends boolean135> = Recipe extends (136 draft: Draft<State>,137 ...rest: infer RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 138why, one step back138) => ValidRecipeReturnType<State>feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 139–141why, one step back139 ? (140 base?: State | undefined,141 ...args: RestArgsMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 142why, one step back142 ) => ReturnTypeWithPatchesIfNeeded<State, UsePatches>feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 143–145why, one step back143 : never // recipe doesn't match initial state144145/**docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 146–164why, one step back146 * 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 {feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 165–169why, one step back165 /** 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 ): Currieddocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 170why, one step back170feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 171–199why, one step back171 /** 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>(docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 200why, one step back200 recipe: Recipe,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 201–202why, one step back201 initialState: State202 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 203–204why, one step back203204 /** Normal producer */feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 205why, one step back205 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 206why, one step back206 base: Base,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 207why, one step back207 recipe: (draft: D) => ValidRecipeReturnType<D>,docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 208why, one step back208 listener?: PatchListenerfeature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 209why, one step back209 ): Basedocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 210–218why, one step back210}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 {feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 219–221why, one step back219 // Types copied from IProduce, wrapped with PatchesTuple220 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>221 <State, Recipe extends Function>(docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 222why, one step back222 recipe: Recipe,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 223–225why, one step back223 initialState: State224 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>225 <Base, D = Draft<Base>>(docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 226why, one step back226 base: Base,fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d
line 227why, one step back227 recipe: (draft: D) => ValidRecipeReturnType<D>,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 228why, one step back228 listener?: PatchListenerfix: produceWithPatches should not wrap result in Immutable, fixes #850, #881Michel Weststratebefore adoptiond8f2636
line 229why, one step back229 ): PatchesTuple<Base>docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 230–231why, one step back230}231feature: Export the type of a recipe as `Produce` (#968)feihebefore adoptionc88f787
lines 232–234why, one step back232/**233 * The type for `recipe function`234 */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 235why, one step back235export 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 back236docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 237–239why, one step back237// 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() {}