Skip to content
Myr

types.ts

This file

Path

src/types.ts

218 lines · 6.3 KiB · TypeScript

Last changed by

Processed bunch of todosMichel Weststratebefore adoption00425e1

History of this file

Take it

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 1–5why, one step back
1import {Nothing, DRAFT_STATE} from "./common"
2import {SetState} from "./set"3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 6why, one step back
6import {ImmerScope} from "./scope"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 7why, one step back
7

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

lines 8–9why, one step back
8export type Objectish = AnyObject | AnyArray | AnyMap | AnySet
9export type ObjectishNoSet = AnyObject | AnyArray | AnyMap

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 10why, one step back
10

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 11–22why, one step back
11export type AnyObject = {[key: string]: any}
12export type AnyArray = Array<any>13export type AnySet = Set<any>14export type AnyMap = Map<any, any>1516export type DraftType =17 | "proxy_object"18 | "proxy_array"19 | "es5_object"20 | "es5_array"21 | "map"22 | "set"

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 23–30why, one step back
23
24export interface ImmerBaseState {25 parent?: ImmerState26 scope: ImmerScope27 modified: boolean28 finalized: boolean29 isManual: boolean30}

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 31–39why, one step back
31
32export type ImmerState =33 | ProxyObjectState34 | ProxyArrayState35 | ES5ObjectState36 | ES5ArrayState37 | MapState38 | SetState39

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 40why, one step back
40// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 41why, one step back
41export type Drafted<Base = any, T extends ImmerState = ImmerState> = {

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 42–43why, one step back
42 [DRAFT_STATE]: T
43} & Base

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 44why, one step back
44

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 45why, one step back
45type Tail<T extends any[]> = ((...t: T) => any) extends (

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 46–47why, one step back
46 _: any,
47 ...tail: infer TT

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 48why, one step back
48) => any

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 49–218why, one step back
49 ? TT
50 : []5152/** Object types that should never be mapped */53type AtomicObject =54 | Function55 | WeakMap<any, any>56 | WeakSet<any>57 | Promise<any>58 | Date59 | RegExp60 | Boolean61 | Number62 | String6364export type Draft<T> = T extends AtomicObject65 ? T66 : T extends Map<infer K, infer V>67 ? DraftMap<K, V>68 : T extends Set<infer V>69 ? DraftSet<V>70 : T extends object71 ? {-readonly [K in keyof T]: Draft<T[K]>}72 : T7374// Inline these in ts 3.775interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}7677// Inline these in ts 3.778interface DraftSet<V> extends Set<Draft<V>> {}7980/** Convert a mutable type into a readonly type */81export type Immutable<T> = T extends AtomicObject82 ? T83 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">84 ? ImmutableMap<K, V>85 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">86 ? ImmutableSet<V>87 : T extends object88 ? {readonly [K in keyof T]: Immutable<T[K]>}89 : T9091interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}9293interface ImmutableSet<V> extends Set<Immutable<V>> {}9495export interface Patch {96 op: "replace" | "remove" | "add"97 path: (string | number)[]98 value?: any99}100101export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void102103/** Converts `nothing` into `undefined` */104type FromNothing<T> = T extends Nothing ? undefined : T105106/** The inferred return type of `produce` */107export type Produced<Base, Return> = Return extends void108 ? Base109 : Return extends Promise<infer Result>110 ? Promise<Result extends void ? Base : FromNothing<Result>>111 : FromNothing<Return>112113/**114 * The `produce` function takes a value and a "recipe function" (whose115 * return value often depends on the base state). The recipe function is116 * free to mutate its first argument however it wants. All mutations are117 * only ever applied to a __copy__ of the base state.118 *119 * Pass only a function to create a "curried producer" which relieves you120 * from passing the recipe function every time.121 *122 * Only plain objects and arrays are made mutable. All other objects are123 * considered uncopyable.124 *125 * Note: This function is __bound__ to its `Immer` instance.126 *127 * @param {any} base - the initial state128 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified129 * @param {Function} patchListener - optional function that will be called with all the patches produced here130 * @returns {any} a new state, or the initial state if nothing was modified131 */132export interface IProduce {133 /** Curried producer */134 <135 Recipe extends (...args: any[]) => any,136 Params extends any[] = Parameters<Recipe>,137 T = Params[0]138 >(139 recipe: Recipe140 ): <Base extends Immutable<T>>(141 base: Base,142 ...rest: Tail<Params>143 ) => Produced<Base, ReturnType<Recipe>>144 // ^ by making the returned type generic, the actual type of the passed in object is preferred145 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe146 // 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!147148 /** Curried producer with initial state */149 <150 Recipe extends (...args: any[]) => any,151 Params extends any[] = Parameters<Recipe>,152 T = Params[0]153 >(154 recipe: Recipe,155 initialState: Immutable<T>156 ): <Base extends Immutable<T>>(157 base?: Base,158 ...rest: Tail<Params>159 ) => Produced<Base, ReturnType<Recipe>>160161 /** Normal producer */162 <Base, D = Draft<Base>, Return = void>(163 base: Base,164 recipe: (draft: D) => Return,165 listener?: PatchListener166 ): Produced<Base, Return>167}168169/**170 * Like `produce`, but instead of just returning the new state,171 * a tuple is returned with [nextState, patches, inversePatches]172 *173 * Like produce, this function supports currying174 */175export interface IProduceWithPatches {176 /** Curried producer */177 <178 Recipe extends (...args: any[]) => any,179 Params extends any[] = Parameters<Recipe>,180 T = Params[0]181 >(182 recipe: Recipe183 ): <Base extends Immutable<T>>(184 base: Base,185 ...rest: Tail<Params>186 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]187 // ^ by making the returned type generic, the actual type of the passed in object is preferred188 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe189 // 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!190191 /** Curried producer with initial state */192 <193 Recipe extends (...args: any[]) => any,194 Params extends any[] = Parameters<Recipe>,195 T = Params[0]196 >(197 recipe: Recipe,198 initialState: Immutable<T>199 ): <Base extends Immutable<T>>(200 base?: Base,201 ...rest: Tail<Params>202 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]203204 /** Normal producer */205 <Base, D = Draft<Base>, Return = void>(206 base: Base,207 recipe: (draft: D) => Return208 ): [Produced<Base, Return>, Patch[], Patch[]]209}210211// Backward compatibility with --target es5212// TODO: still needed?213// declare global {214// interface Set<T> {}215// interface Map<K, V> {}216// interface WeakSet<T> {}217// interface WeakMap<K extends object, V> {}218// }