mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
53 lines (52 loc) • 1.98 kB
TypeScript
import { propsTypeSymbol } from '../modelShared/BaseModelShared';
import { ModelProps, ModelPropsToTransformedCreationData, ModelPropsToUntransformedCreationData, ModelPropsToUntransformedData } from '../modelShared/prop';
import { TypeCheckError } from '../types/TypeCheckError';
/**
* Base abstract class for data models. Use `DataModel` instead when extending.
*
* Never override the constructor, use `onLazyInit` instead.
*
* @template Data Props data type.
*/
export declare abstract class BaseDataModel<TProps extends ModelProps> {
[propsTypeSymbol]: TProps;
/**
* Called after the instance is created when there's the first call to `fn(M, data)`.
*/
protected onLazyInit?(): void;
/**
* Data part of the model, which is observable and will be serialized in snapshots.
* Use it if one of the data properties matches one of the model properties/functions.
* This also allows access to the backed values of transformed properties.
*/
readonly $: ModelPropsToUntransformedData<TProps>;
/**
* Performs a type check over the model instance.
* For this to work a data type has to be declared as part of the model properties.
*
* @returns A `TypeCheckError` or `null` if there is no error.
*/
typeCheck(): TypeCheckError | null;
/**
* Creates an instance of a data model.
*/
constructor(data: ModelPropsToUntransformedCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>);
toString(options?: {
withData?: boolean;
}): string;
}
/**
* @ignore
*/
export type BaseDataModelKeys = keyof AnyDataModel | "onLazyInit";
/**
* Any kind of data model instance.
*/
export interface AnyDataModel extends BaseDataModel<any> {
}
/**
* A data model class declaration, made of a base model and the model interface.
*/
export type DataModelClassDeclaration<BaseModelClass, ModelInterface> = BaseModelClass & {
(...args: any[]): ModelInterface;
};