immutable-class
Version:
A template for creating immutable classes
65 lines • 2.65 kB
TypeScript
export type Validator = (x: any) => void;
export interface ImmutableLike {
fromJS: (js: any, context?: any) => any;
}
export type PropertyType = 'date' | 'array';
export declare const PropertyType: {
DATE: PropertyType;
ARRAY: PropertyType;
};
export interface Property<T extends Record<string, any> = any> {
name: keyof T & string;
defaultValue?: any;
possibleValues?: readonly any[];
validate?: Validator | Validator[];
type?: PropertyType;
immutableClass?: ImmutableLike;
immutableClassArray?: ImmutableLike;
equal?: (a: any, b: any) => boolean;
toJS?: (v: any) => any;
contextTransform?: (context: Record<string, any>) => Record<string, any>;
preserveUndefined?: boolean;
emptyArrayIsOk?: boolean;
}
export interface ClassFnType {
PROPERTIES: Property[];
fromJS(properties: any, context?: any): any;
new (properties: any): any;
}
export interface ImmutableInstanceType<ValueType, JSType> {
valueOf(): ValueType;
toJS(): JSType;
toJSON(): JSType;
toString(): string;
equals(other: ImmutableInstanceType<ValueType, JSType> | undefined): boolean;
}
export interface BackCompat {
condition: (js: any) => boolean;
action: (js: any) => void;
}
export declare abstract class BaseImmutable<ValueType extends Record<string, any>, JSType> implements ImmutableInstanceType<ValueType, JSType> {
static jsToValue<T extends Record<string, any> = any>(properties: Property<T>[], js: any, backCompats?: BackCompat[], context?: Record<string, any>): any;
static finalize(ClassFn: ClassFnType): void;
static ensure: {
number: (n: any) => void;
positive: (n: any) => void;
nonNegative: (n: any) => void;
};
constructor(value: ValueType);
ownProperties(): Property<ValueType>[];
findOwnProperty(propName: keyof ValueType & string): Property | undefined;
hasProperty(propName: keyof ValueType & string): boolean;
valueOf(): ValueType;
toJS(): JSType;
toJSON(): JSType;
toString(): string;
getDifference(other: BaseImmutable<ValueType, JSType> | undefined, returnOnFirstDifference?: boolean): string[];
equals(other: BaseImmutable<ValueType, JSType> | undefined): boolean;
equivalent(other: BaseImmutable<ValueType, JSType>): boolean;
get<T extends keyof ValueType & string>(propName: T): ValueType[T];
change<T extends keyof ValueType & string>(propName: T, newValue: ValueType[T]): this;
changeMany(properties: Partial<ValueType>): this;
deepChange(propName: string, newValue: any): this;
deepGet(propName: string): any;
}
//# sourceMappingURL=base-immutable.d.ts.map