Skip to content
Myr

index.ts

This file

Path

src/index.ts

110 lines · 3.2 KiB · TypeScript

Last changed by

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

History of this file

Take it

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 1–7why, one step back
1import {
2 IProduce,3 IProduceWithPatches,4 Immer,5 Draft,6 Immutable7} from "./internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 8why, one step back
8

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 9–19why, one step back
9export {
10 Draft,11 Immutable,12 Patch,13 PatchListener,14 original,15 isDraft,16 isDraftable,17 NOTHING as nothing,18 DRAFTABLE as immerable19} from "./internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 20–89why, one step back
20
21const immer = new Immer()2223/**24 * The `produce` function takes a value and a "recipe function" (whose25 * return value often depends on the base state). The recipe function is26 * free to mutate its first argument however it wants. All mutations are27 * only ever applied to a __copy__ of the base state.28 *29 * Pass only a function to create a "curried producer" which relieves you30 * from passing the recipe function every time.31 *32 * Only plain objects and arrays are made mutable. All other objects are33 * considered uncopyable.34 *35 * Note: This function is __bound__ to its `Immer` instance.36 *37 * @param {any} base - the initial state38 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified39 * @param {Function} patchListener - optional function that will be called with all the patches produced here40 * @returns {any} a new state, or the initial state if nothing was modified41 */42export const produce: IProduce = immer.produce43export default produce4445/**46 * Like `produce`, but `produceWithPatches` always returns a tuple47 * [nextState, patches, inversePatches] (instead of just the next state)48 */49export const produceWithPatches: IProduceWithPatches = immer.produceWithPatches.bind(50 immer51)5253/**54 * Pass true to automatically freeze all copies created by Immer.55 *56 * By default, auto-freezing is disabled in production.57 */58export const setAutoFreeze = immer.setAutoFreeze.bind(immer)5960/**61 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is62 * always faster than using ES5 proxies.63 *64 * By default, feature detection is used, so calling this is rarely necessary.65 */66export const setUseProxies = immer.setUseProxies.bind(immer)6768/**69 * Apply an array of Immer patches to the first argument.70 *71 * This function is a producer, which means copy-on-write is in effect.72 */73export const applyPatches = immer.applyPatches.bind(immer)7475/**76 * Create an Immer draft from the given base state, which may be a draft itself.77 * The draft can be modified until you finalize it with the `finishDraft` function.78 */79export const createDraft = immer.createDraft.bind(immer)8081/**82 * Finalize an Immer draft from a `createDraft` call, returning the base state83 * (if no changes were made) or a modified copy. The draft must *not* be84 * mutated afterwards.85 *86 * Pass a function as the 2nd argument to generate Immer patches based on the87 * changes that were made.88 */89export const finishDraft = immer.finishDraft.bind(immer)

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 90–108why, one step back
90
91/**92 * This function is actually a no-op, but can be used to cast an immutable type93 * to an draft type and make TypeScript happy94 *95 * @param value96 */97export function castDraft<T>(value: T): Draft<T> {98 return value as any99}100101/**102 * This function is actually a no-op, but can be used to cast a mutable type103 * to an immutable type and make TypeScript happy104 * @param value105 */106export function castImmutable<T>(value: T): Immutable<T> {107 return value as any108}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 109–110why, one step back
109
110export {Immer}