Skip to content
Myr

Merge pull request #541 from phryneas/typesversions-compat

Before adoption: this commit came in with the history the project adopted, so no landing records it.

add 5.0.2 type definitions as fallback for TS<3.7

authored by Michel Weststrate · committed by GitHub · parents 5f86272, f215409 · Browse files · 3 files+321 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+321 added−1 removed

  • other:+321 added−1 removed

3 files+321 added−1 removed

added:compat/pre-3.7/dist/index.d.ts+307 added
1type Tail<T extends any[]> = ((...t: T) => any) extends (2 _: any,3 ...tail: infer TT4) => any5 ? TT6 : []78/** Object types that should never be mapped */9type AtomicObject =10 | Function11 | WeakMap<any, any>12 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String1920export type Draft<T> = T extends AtomicObject21 ? T22 : T extends Map<infer K, infer V>23 ? DraftMap<K, V>24 : T extends Set<infer V>25 ? DraftSet<V>26 : T extends object27 ? {-readonly [K in keyof T]: Draft<T[K]>}28 : T2930// Inline these in ts 3.731interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3233// Inline these in ts 3.734interface DraftSet<V> extends Set<Draft<V>> {}3536/** Convert a mutable type into a readonly type */37export type Immutable<T> = T extends AtomicObject38 ? T39 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">40 ? ImmutableMap<K, V>41 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">42 ? ImmutableSet<V>43 : T extends object44 ? {readonly [K in keyof T]: Immutable<T[K]>}45 : T4647interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}4849interface ImmutableSet<V> extends Set<Immutable<V>> {}5051export interface Patch {52 op: "replace" | "remove" | "add"53 path: (string | number)[]54 value?: any55}5657export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void5859/** Converts `nothing` into `undefined` */60type FromNothing<T> = T extends Nothing ? undefined : T6162/** The inferred return type of `produce` */63export type Produced<Base, Return> = Return extends void64 ? Base65 : Return extends Promise<infer Result>66 ? Promise<Result extends void ? Base : FromNothing<Result>>67 : FromNothing<Return>6869/**70 * The `produce` function takes a value and a "recipe function" (whose71 * return value often depends on the base state). The recipe function is72 * free to mutate its first argument however it wants. All mutations are73 * only ever applied to a __copy__ of the base state.74 *75 * Pass only a function to create a "curried producer" which relieves you76 * from passing the recipe function every time.77 *78 * Only plain objects and arrays are made mutable. All other objects are79 * considered uncopyable.80 *81 * Note: This function is __bound__ to its `Immer` instance.82 *83 * @param {any} base - the initial state84 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified85 * @param {Function} patchListener - optional function that will be called with all the patches produced here86 * @returns {any} a new state, or the initial state if nothing was modified87 */88export interface IProduce {89 /** Curried producer */90 <91 Recipe extends (...args: any[]) => any,92 Params extends any[] = Parameters<Recipe>,93 T = Params[0]94 >(95 recipe: Recipe96 ): <Base extends Immutable<T>>(97 base: Base,98 ...rest: Tail<Params>99 ) => Produced<Base, ReturnType<Recipe>>100 // ^ by making the returned type generic, the actual type of the passed in object is preferred101 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe102 // 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!103104 /** Curried producer with initial state */105 <106 Recipe extends (...args: any[]) => any,107 Params extends any[] = Parameters<Recipe>,108 T = Params[0]109 >(110 recipe: Recipe,111 initialState: Immutable<T>112 ): <Base extends Immutable<T>>(113 base?: Base,114 ...rest: Tail<Params>115 ) => Produced<Base, ReturnType<Recipe>>116117 /** Normal producer */118 <Base, D = Draft<Base>, Return = void>(119 base: Base,120 recipe: (draft: D) => Return,121 listener?: PatchListener122 ): Produced<Base, Return>123}124125export const produce: IProduce126export default produce127128/**129 * Like `produce`, but instead of just returning the new state,130 * a tuple is returned with [nextState, patches, inversePatches]131 *132 * Like produce, this function supports currying133 */134export interface IProduceWithPatches {135 /** Curried producer */136 <137 Recipe extends (...args: any[]) => any,138 Params extends any[] = Parameters<Recipe>,139 T = Params[0]140 >(141 recipe: Recipe142 ): <Base extends Immutable<T>>(143 base: Base,144 ...rest: Tail<Params>145 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]146 // ^ by making the returned type generic, the actual type of the passed in object is preferred147 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe148 // 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!149150 /** Curried producer with initial state */151 <152 Recipe extends (...args: any[]) => any,153 Params extends any[] = Parameters<Recipe>,154 T = Params[0]155 >(156 recipe: Recipe,157 initialState: Immutable<T>158 ): <Base extends Immutable<T>>(159 base?: Base,160 ...rest: Tail<Params>161 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]162163 /** Normal producer */164 <Base, D = Draft<Base>, Return = void>(165 base: Base,166 recipe: (draft: D) => Return167 ): [Produced<Base, Return>, Patch[], Patch[]]168}169export const produceWithPatches: IProduceWithPatches170171/** Use a class type for `nothing` so its type is unique */172declare class Nothing {173 // This lets us do `Exclude<T, Nothing>`174 private _: any175}176177/**178 * The sentinel value returned by producers to replace the draft with undefined.179 */180export const nothing: Nothing181182/**183 * To let Immer treat your class instances as plain immutable objects184 * (albeit with a custom prototype), you must define either an instance property185 * or a static property on each of your custom classes.186 *187 * Otherwise, your class instance will never be drafted, which means it won't be188 * safe to mutate in a produce callback.189 */190export const immerable: unique symbol191192/**193 * Pass true to automatically freeze all copies created by Immer.194 *195 * By default, auto-freezing is disabled in production.196 */197export function setAutoFreeze(autoFreeze: boolean): void198199/**200 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is201 * always faster than using ES5 proxies.202 *203 * By default, feature detection is used, so calling this is rarely necessary.204 */205export function setUseProxies(useProxies: boolean): void206207/**208 * Apply an array of Immer patches to the first argument.209 *210 * This function is a producer, which means copy-on-write is in effect.211 */212export function applyPatches<S>(base: S, patches: Patch[]): S213214/**215 * Create an Immer draft from the given base state, which may be a draft itself.216 * The draft can be modified until you finalize it with the `finishDraft` function.217 */218export function createDraft<T>(base: T): Draft<T>219220/**221 * Finalize an Immer draft from a `createDraft` call, returning the base state222 * (if no changes were made) or a modified copy. The draft must *not* be223 * mutated afterwards.224 *225 * Pass a function as the 2nd argument to generate Immer patches based on the226 * changes that were made.227 */228export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>229230/** Get the underlying object that is represented by the given draft */231export function original<T>(value: T): T | void232233/** Returns true if the given value is an Immer draft */234export function isDraft(value: any): boolean235236/** Returns true if the given value can be drafted by Immer */237export function isDraftable(value: any): boolean238239export class Immer {240 constructor(config: {241 useProxies?: boolean242 autoFreeze?: boolean243 onAssign?: (244 state: ImmerState,245 prop: string | number,246 value: unknown247 ) => void248 onDelete?: (state: ImmerState, prop: string | number) => void249 onCopy?: (state: ImmerState) => void250 })251 /**252 * The `produce` function takes a value and a "recipe function" (whose253 * return value often depends on the base state). The recipe function is254 * free to mutate its first argument however it wants. All mutations are255 * only ever applied to a __copy__ of the base state.256 *257 * Pass only a function to create a "curried producer" which relieves you258 * from passing the recipe function every time.259 *260 * Only plain objects and arrays are made mutable. All other objects are261 * considered uncopyable.262 *263 * Note: This function is __bound__ to its `Immer` instance.264 *265 * @param {any} base - the initial state266 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified267 * @param {Function} patchListener - optional function that will be called with all the patches produced here268 * @returns {any} a new state, or the initial state if nothing was modified269 */270 produce: IProduce271 /**272 * When true, `produce` will freeze the copies it creates.273 */274 readonly autoFreeze: boolean275 /**276 * When true, drafts are ES2015 proxies.277 */278 readonly useProxies: boolean279 /**280 * Pass true to automatically freeze all copies created by Immer.281 *282 * By default, auto-freezing is disabled in production.283 */284 setAutoFreeze(autoFreeze: boolean): void285 /**286 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is287 * always faster than using ES5 proxies.288 *289 * By default, feature detection is used, so calling this is rarely necessary.290 */291 setUseProxies(useProxies: boolean): void292}293294export interface ImmerState<T = any> {295 parent?: ImmerState296 base: T297 copy: T298 assigned: {[prop: string]: boolean; [index: number]: boolean}299}300301// Backward compatibility with --target es5302declare global {303 interface Set<T> {}304 interface Map<K, V> {}305 interface WeakSet<T> {}306 interface WeakMap<K extends object, V> {}307}
modified:.gitignore+1 added-1 removed
56 unmodified lines
57# dotenv environment variables file58.env5960.idea61dist/61/dist/62website/build63.rts2*
modified:package.json+13 added