Skip to content

Instantly share code, notes, and snippets.

@garkin
Created August 22, 2017 06:07
Show Gist options
  • Save garkin/db4ad0efd20a75a5ada3726cc783173f to your computer and use it in GitHub Desktop.
Save garkin/db4ad0efd20a75a5ada3726cc783173f to your computer and use it in GitHub Desktop.
// https://github.com/kolodny/immutability-helper
/** Declaration extension point */
declare namespace ImmutabilityHelper {
interface CustomOperators<T> {
/* $someShit?: T */
/* $moreShit?: T */
}
}
declare module "immutability-helper" {
function update<T>(data: T, query: Query<T>): T;
namespace update {
function newContext(): typeof update;
function extend<T>(command: string, handler: (param: any, old: T) => T)
}
type ObjectOperator<T> =
/** replace the target entirely */
| { $set: T }
/** remove the list of keys in array from the target object */
| { $unset: string[] }
/** merge the keys of object with the target */
| { $merge: Partial<T> }
/** passes in the current value to the function and updates it with the new returned value. */
| { $apply: (old: T) => T };
type ArrayOperator<T> =
/** push() all the items in array on the target. */
| { $push: T }
/** unshift() all the items in array on the target */
| { $unshift: T }
/** for each item in arrays call splice() on the target with the parameters provided by the item.
Note: The items in the array are applied sequentially, so the order matters.
The indices of the target may change during the operation. */
| { $splice: [number, number][] };
type Operators<T> =
| ObjectOperator<T>
| ArrayOperator<T>
| ImmutabilityHelper.CustomOperators<T>;
type Tree<T> = { [K in keyof T]?: Tree<T[K]> | Operators<T[K]> };
type Query<T> = Tree<T> | Operators<T>;
export = update;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment