Skip to content
Myr

types-external.ts

This file

Path

src/types/types-external.ts

264 lines · 8.2 KiB · TypeScript

Last changed by

fix: unnecessarily recursive Draft type (#990)Homa Wongbefore adoptionb9eae1d

History of this file

Take it

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 1–2why, one step back
1import {Nothing} from "../internal"
2

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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,

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

lines 14–27why, 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/**

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 28–30why, one step back
28 * These should also never be mapped but must be tested after regular Map and
29 * Set30 */

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

line 31why, one step back
31type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 32why, one step back
32

fix: unnecessarily recursive Draft type (#990)Homa Wongbefore adoptionb9eae1d

lines 33–35why, one step back
33export type WritableDraft<T> = {
34 -readonly [K in keyof T]: T[K] extends object ? Draft<T[K]> : T[K]35}

fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0

line 36why, one step back
36

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

line 37why, one step back
37/** 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 38why, one step back
38export type Draft<T> = T extends PrimitiveType

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 39why, one step back
39 ? T

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

lines 40–41why, one step back
40 : T extends AtomicObject
41 ? T

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 45–48why, one step back
45 ? Set<Draft<V>>
46 : T extends WeakReferences47 ? T48 : T extends object

fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0

line 49why, one step back
49 ? WritableDraft<T>

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 50–52why, one step back
50 : T
5152/** Convert a mutable type into a readonly type */

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

lines 53–55why, one step back
53export type Immutable<T> = T extends PrimitiveType
54 ? T55 : T extends AtomicObject

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 56why, one step back
56 ? T

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 60–85why, one step back
60 ? ReadonlySet<Immutable<V>>
61 : T extends WeakReferences62 ? T63 : T extends object64 ? {readonly [K in keyof T]: Immutable<T[K]>}65 : T6667export interface Patch {68 op: "replace" | "remove" | "add"69 path: (string | number)[]70 value?: any71}7273export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7475/** Converts `nothing` into `undefined` */76type FromNothing<T> = T extends Nothing ? undefined : T7778/** The inferred return type of `produce` */79export type Produced<Base, Return> = Return extends void80 ? Base81 : Return extends Promise<infer Result>82 ? Promise<Result extends void ? Base : FromNothing<Result>>83 : FromNothing<Return>8485/**

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 86–158why, one step back
86 * Utility types
87 */88type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8990type ValidRecipeReturnType<State> =91 | State92 | void93 | undefined94 | (State extends undefined ? Nothing : never)9596type ValidRecipeReturnTypePossiblyPromise<State> =97 | ValidRecipeReturnType<State>98 | Promise<ValidRecipeReturnType<State>>99100type PromisifyReturnIfNeeded<101 State,102 Recipe extends AnyFunc,103 UsePatches extends boolean104> = ReturnType<Recipe> extends Promise<any>105 ? Promise<UsePatches extends true ? PatchesTuple<State> : State>106 : UsePatches extends true107 ? PatchesTuple<State>108 : State109110/**111 * Core Producer inference112 */113type InferRecipeFromCurried<Curried> = Curried extends (114 base: infer State,115 ...rest: infer Args116) => any // extra assertion to make sure this is a proper curried function (state, args) => state117 ? ReturnType<Curried> extends State118 ? (119 draft: Draft<State>,120 ...rest: Args121 ) => ValidRecipeReturnType<Draft<State>>122 : never123 : never124125type InferInitialStateFromCurried<Curried> = Curried extends (126 base: infer State,127 ...rest: any[]128) => any // extra assertion to make sure this is a proper curried function (state, args) => state129 ? State130 : never131132type InferCurriedFromRecipe<133 Recipe,134 UsePatches extends boolean135> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type136 ? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState>137 ? (138 base: Immutable<DraftState>,139 ...args: RestArgs140 ) => PromisifyReturnIfNeeded<DraftState, Recipe, 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 either141 : never // incorrect return type142 : never // not a function143144type InferCurriedFromInitialStateAndRecipe<145 State,146 Recipe,147 UsePatches extends boolean148> = Recipe extends (149 draft: Draft<State>,150 ...rest: infer RestArgs151) => ValidRecipeReturnTypePossiblyPromise<State>152 ? (153 base?: State | undefined,154 ...args: RestArgs155 ) => PromisifyReturnIfNeeded<State, Recipe, UsePatches>156 : never // recipe doesn't match initial state157158/**

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 159–177why, one step back
159 * The `produce` function takes a value and a "recipe function" (whose
160 * return value often depends on the base state). The recipe function is161 * free to mutate its first argument however it wants. All mutations are162 * only ever applied to a __copy__ of the base state.163 *164 * Pass only a function to create a "curried producer" which relieves you165 * from passing the recipe function every time.166 *167 * Only plain objects and arrays are made mutable. All other objects are168 * considered uncopyable.169 *170 * Note: This function is __bound__ to its `Immer` instance.171 *172 * @param {any} base - the initial state173 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified174 * @param {Function} patchListener - optional function that will be called with all the patches produced here175 * @returns {any} a new state, or the initial state if nothing was modified176 */177export interface IProduce {

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 178–182why, one step back
178 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */
179 <Curried>(180 recipe: InferRecipeFromCurried<Curried>,181 initialState?: InferInitialStateFromCurried<Curried>182 ): Curried

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 183why, one step back
183

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 184–212why, one step back
184 /** Curried producer that infers curried from the recipe */
185 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<186 Recipe,187 false188 >189190 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */191 <State>(192 recipe: (193 state: Draft<State>,194 initialState: State195 ) => ValidRecipeReturnType<State>196 ): (state?: State) => State197 <State, Args extends any[]>(198 recipe: (199 state: Draft<State>,200 ...args: Args201 ) => ValidRecipeReturnType<State>,202 initialState: State203 ): (state?: State, ...args: Args) => State204 <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (205 state: State206 ) => State207 <State, Args extends any[]>(208 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>209 ): (state: State, ...args: Args) => State210211 /** Curried producer with initial state, infers recipe from initial state */212 <State, Recipe extends Function>(

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 216–217why, one step back
216
217 /** Normal producer */

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

line 222why, one step back
222 ): Base

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 223why, one step back
223

chore: typo dormal -> normal (#910)Jacob Eldredbefore adoption49f94e1

line 224why, one step back
224 /** Promisified normal producer */

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 225–229why, one step back
225 <Base, D = Draft<Base>>(
226 base: Base,227 recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,228 listener?: PatchListener229 ): Promise<Base>

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 230–238why, one step back
230}
231232/**233 * Like `produce`, but instead of just returning the new state,234 * a tuple is returned with [nextState, patches, inversePatches]235 *236 * Like produce, this function supports currying237 */238export interface IProduceWithPatches {

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 239–241why, one step back
239 // Types copied from IProduce, wrapped with PatchesTuple
240 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>241 <State, Recipe extends Function>(

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 243–245why, one step back
243 initialState: State
244 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>245 <Base, D = Draft<Base>>(

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

lines 250–251why, one step back
250 <Base, D = Draft<Base>>(
251 base: Base,

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

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

feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e

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

fix: incorrect return type for async produceWithPatches (#933)Myles Jbefore adoption9f7623d

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 255–256why, one step back
255}
256

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

lines 257–261why, one step back
257/**
258 * The type for `recipe function`259 */260export type Producer<T> = (draft: Draft<T>) => ValidRecipeReturnType<Draft<T>> | Promise<ValidRecipeReturnType<Draft<T>>>261

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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