mobx-state-tree
Version:
Opinionated, transactional, MobX powered state container
61 lines (60 loc) • 2.19 kB
TypeScript
import { IType } from "../../internal";
export interface CustomTypeOptions<S, T> {
/** Friendly name */
name: string;
/** given a serialized value and environment, how to turn it into the target type */
fromSnapshot(snapshot: S, env?: any): T;
/** return the serialization of the current value */
toSnapshot(value: T): S;
/** if true, this is a converted value, if false, it's a snapshot */
isTargetType(value: T | S): boolean;
/** a non empty string is assumed to be a validation error */
getValidationMessage(snapshot: S): string;
}
/**
* `types.custom` - Creates a custom type. Custom types can be used for arbitrary immutable values, that have a serializable representation. For example, to create your own Date representation, Decimal type etc.
*
* The signature of the options is:
* ```ts
* export interface CustomTypeOptions<S, T> {
* // Friendly name
* name: string
* // given a serialized value and environment, how to turn it into the target type
* fromSnapshot(snapshot: S, env: any): T
* // return the serialization of the current value
* toSnapshot(value: T): S
* // if true, this is a converted value, if false, it's a snapshot
* isTargetType(value: T | S): value is T
* // a non empty string is assumed to be a validation error
* getValidationMessage?(snapshot: S): string
* }
* ```
*
* Example:
* ```ts
* const DecimalPrimitive = types.custom<string, Decimal>({
* name: "Decimal",
* fromSnapshot(value: string) {
* return new Decimal(value)
* },
* toSnapshot(value: Decimal) {
* return value.toString()
* },
* isTargetType(value: string | Decimal): boolean {
* return value instanceof Decimal
* },
* getValidationMessage(value: string): string {
* if (/^-?\d+\.\d+$/.test(value)) return "" // OK
* return `'${value}' doesn't look like a valid decimal number`
* }
* })
*
* const Wallet = types.model({
* balance: DecimalPrimitive
* })
* ```
*
* @param options
* @returns
*/
export declare function custom<S, T>(options: CustomTypeOptions<S, T>): IType<S | T, S, T>;