types-external.ts
This file
Path
src/types/types-external.ts
187 lines · 5.9 KiB · TypeScript
Last changed by
fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–9why, one step back1import {Nothing} from "../internal"23type Tail<T extends any[]> = ((...t: T) => any) extends (4 _: any,5 ...tail: infer TT6) => any7 ? TT8 : []9fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 10–11why, one step back10type PrimitiveType = number | string | boolean11Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 12why, one step back12/** Object types that should never be mapped */fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
line 13why, one step back13type AtomicObject = Function | Promise<any> | Date | RegExpMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 14–15why, one step back1415/**fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
lines 16–32why, one step back16 * If the lib "ES2105.collections" is not included in tsconfig.json,17 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)18 * or `{}` (from the node types), in both cases entering an infite recursion in19 * pattern matching type mappings20 * This type can be used to cast these types to `void` in these cases.21 */22export type IfAvailable<T, Fallback = void> =23 // fallback if any24 true | false extends (T extends never25 ? true26 : false)27 ? Fallback // fallback if empty type28 : keyof T extends never29 ? Fallback // original type30 : T3132/**Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 33–35why, one step back33 * These should also never be mapped but must be tested after regular Map and34 * Set35 */fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 36why, one step back36type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 37why, one step back37fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
lines 38–39why, one step back38export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}39fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
line 40why, one step back40export type Draft<T> = T extends PrimitiveTypeMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 41why, one step back41 ? Tfix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 42–43why, one step back42 : T extends AtomicObject43 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 44why, one step back44 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 45why, one step back45 ? Map<Draft<K>, Draft<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 46why, one step back46 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 47–50why, one step back47 ? Set<Draft<V>>48 : T extends WeakReferences49 ? T50 : T extends objectfix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
line 51why, one step back51 ? WritableDraft<T>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 52–54why, one step back52 : T5354/** Convert a mutable type into a readonly type */fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 55–57why, one step back55export type Immutable<T> = T extends PrimitiveType56 ? T57 : T extends AtomicObjectMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 58why, one step back58 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 59why, one step back59 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 60why, one step back60 ? ReadonlyMap<Immutable<K>, Immutable<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 61why, one step back61 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 62–187why, one step back62 ? ReadonlySet<Immutable<V>>63 : T extends WeakReferences64 ? T65 : T extends object66 ? {readonly [K in keyof T]: Immutable<T[K]>}67 : T6869export interface Patch {70 op: "replace" | "remove" | "add"71 path: (string | number)[]72 value?: any73}7475export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7677/** Converts `nothing` into `undefined` */78type FromNothing<T> = T extends Nothing ? undefined : T7980/** The inferred return type of `produce` */81export type Produced<Base, Return> = Return extends void82 ? Base83 : Return extends Promise<infer Result>84 ? Promise<Result extends void ? Base : FromNothing<Result>>85 : FromNothing<Return>8687/**88 * The `produce` function takes a value and a "recipe function" (whose89 * return value often depends on the base state). The recipe function is90 * free to mutate its first argument however it wants. All mutations are91 * only ever applied to a __copy__ of the base state.92 *93 * Pass only a function to create a "curried producer" which relieves you94 * from passing the recipe function every time.95 *96 * Only plain objects and arrays are made mutable. All other objects are97 * considered uncopyable.98 *99 * Note: This function is __bound__ to its `Immer` instance.100 *101 * @param {any} base - the initial state102 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified103 * @param {Function} patchListener - optional function that will be called with all the patches produced here104 * @returns {any} a new state, or the initial state if nothing was modified105 */106export interface IProduce {107 /** Curried producer */108 <109 Recipe extends (...args: any[]) => any,110 Params extends any[] = Parameters<Recipe>,111 T = Params[0]112 >(113 recipe: Recipe114 ): <Base extends Immutable<T>>(115 base: Base,116 ...rest: Tail<Params>117 ) => Produced<Base, ReturnType<Recipe>>118 // ^ by making the returned type generic, the actual type of the passed in object is preferred119 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe120 // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!121122 /** Curried producer with initial state */123 <124 Recipe extends (...args: any[]) => any,125 Params extends any[] = Parameters<Recipe>,126 T = Params[0]127 >(128 recipe: Recipe,129 initialState: Immutable<T>130 ): <Base extends Immutable<T>>(131 base?: Base,132 ...rest: Tail<Params>133 ) => Produced<Base, ReturnType<Recipe>>134135 /** Normal producer */136 <Base, D = Draft<Base>, Return = void>(137 base: Base,138 recipe: (draft: D) => Return,139 listener?: PatchListener140 ): Produced<Base, Return>141}142143/**144 * Like `produce`, but instead of just returning the new state,145 * a tuple is returned with [nextState, patches, inversePatches]146 *147 * Like produce, this function supports currying148 */149export interface IProduceWithPatches {150 /** Curried producer */151 <152 Recipe extends (...args: any[]) => any,153 Params extends any[] = Parameters<Recipe>,154 T = Params[0]155 >(156 recipe: Recipe157 ): <Base extends Immutable<T>>(158 base: Base,159 ...rest: Tail<Params>160 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]161 // ^ by making the returned type generic, the actual type of the passed in object is preferred162 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe163 // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!164165 /** Curried producer with initial state */166 <167 Recipe extends (...args: any[]) => any,168 Params extends any[] = Parameters<Recipe>,169 T = Params[0]170 >(171 recipe: Recipe,172 initialState: Immutable<T>173 ): <Base extends Immutable<T>>(174 base?: Base,175 ...rest: Tail<Params>176 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]177178 /** Normal producer */179 <Base, D = Draft<Base>, Return = void>(180 base: Base,181 recipe: (draft: D) => Return182 ): [Produced<Base, Return>, Patch[], Patch[]]183}184185// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..186// hopefully it get's tree-shaken away for everyone :)187export function never_used() {}