Skip to content
Myr

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 file

Take it

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

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 2why, one step back
2

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

Fix typo (#985)ddronebefore adoptionba0a79a

line 11why, one step back
11 * If the lib "ES2015.Collection" is not included in tsconfig.json,

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 12why, one step back
12 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)

Fix typo (#985)ddronebefore adoptionba0a79a

line 13why, one step back
13 * or `{}` (from the node types), in both cases entering an infinite recursion in

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 14–32why, one step back
14 * pattern matching type mappings
15 * 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>>32

fix: revert earlier fix (#990) for recursive types (#1014)Nathan Bieremabefore adoption3eeb331

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 34why, one step back
34

chore: Add note explaining Draft type (#967)Stephen Michelbefore adoption54ecc9b

line 35why, one step back
35/** Convert a readonly type into a mutable type, if possible */

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 37why, one step back
37 ? T

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

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

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 43–50why, one step back
43 ? Set<Draft<V>>
44 : T extends WeakReferences45 ? T46 : T extends object47 ? WritableDraft<T>48 : T4950/** Convert a mutable type into a readonly type */

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 54why, one step back
54 ? T

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 58–73why, one step back
58 ? 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 back
74type FromNothing<T> = T extends typeof NOTHING ? undefined : T

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 75–81why, one step back
75
76/** 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 back
82 * Utility types
83 */84type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8586type ValidRecipeReturnType<State> =87 | State88 | void89 | undefined

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 91why, one step back
91

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

line 92why, one step back
92type ReturnTypeWithPatchesIfNeeded<

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 93–94why, one step back
93 State,
94 UsePatches extends boolean

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 96–122why, one step back
96
97/**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 type

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 124–126why, one step back
124 ? (
125 base: Immutable<DraftState>,126 ...args: RestArgs

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 128–137why, one step back
128 : never // incorrect return type
129 : never // not a function130131type InferCurriedFromInitialStateAndRecipe<132 State,133 Recipe,134 UsePatches extends boolean135> = Recipe extends (136 draft: Draft<State>,137 ...rest: infer RestArgs

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 139–141why, one step back
139 ? (
140 base?: State | undefined,141 ...args: RestArgs

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 143–145why, one step back
143 : never // recipe doesn't match initial state
144145/**

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 146–164why, one step back
146 * The `produce` function takes a value and a "recipe function" (whose
147 * 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 back
165 /** 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 ): Curried

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 170why, one step back
170

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 171–199why, one step back
171 /** 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>(

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 203–204why, one step back
203
204 /** Normal producer */

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 209why, one step back
209 ): Base

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 210–218why, one step back
210}
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 back
219 // Types copied from IProduce, wrapped with PatchesTuple
220 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>221 <State, Recipe extends Function>(

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 223–225why, one step back
223 initialState: State
224 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>225 <Base, D = Draft<Base>>(

chore(comments): update comments (#727)郗银before adoptionfe8f589

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

fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

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

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

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 230–231why, one step back
230}
231

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

lines 232–234why, one step back
232/**
233 * The type for `recipe function`234 */

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

line 235why, one step back
235export 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 back
236

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 237–239why, one step back
237// 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() {}