immutable-typescript
Version:
Immutable objects for TypeScript
27 lines (26 loc) • 1.36 kB
TypeScript
export declare type ImmutableWrapper<T> = T extends any[] ? ReadonlyArray<Immutable<T[0]>> : T extends () => {} ? () => ImmutableWrapper<ReturnType<T>> : Immutable<T>;
export declare type Immutable<T> = {
readonly [P in keyof T]: ImmutableWrapper<T[P]>;
};
export declare type PropOrIndex<T> = T extends any[] ? number : keyof T;
export declare type ProxyWrapper<T, P, R> = T extends any[] ? ArrayProxy<T, P, R> : string extends keyof T ? DictProxy<T, P, R> : Proxy<T, P, R>;
export declare class Proxy<T, P, R> {
protected proxied: Immutable<T>;
protected parentProxy: Proxy<P, any, R>;
protected prop: any;
constructor(proxied: Immutable<T>, parentProxy: Proxy<P, any, R>, prop: any);
at<K extends PropOrIndex<T>>(prop: K): ProxyWrapper<T[K & keyof T], T, R>;
set<K extends PropOrIndex<T>>(prop: K, val: ImmutableWrapper<T[K & keyof T]>): Immutable<R>;
protected plugInObject(copy: any): Immutable<R>;
}
export declare class ArrayProxy<T, P, R> extends Proxy<T, P, R> {
unshift(...elements: (T | Immutable<T>)[]): Immutable<R>;
remove(index: number, count: number): Immutable<R>;
}
export declare class DictProxy<T, P, R> extends Proxy<T, P, R> {
del(): void;
}
export declare class ImmutableUtils {
static asImmutable<T>(obj: T): Immutable<T>;
static update<T>(obj: Immutable<T>): Proxy<T, never, T>;
}