UNPKG

5.02 kBTypeScriptView Raw
1import { IValidationContext, IValidationResult, IStateTreeNode, ModelPrimitive } from "../../internal";
2/**
3 * A state tree node value.
4 * @hidden
5 */
6export declare type STNValue<T, IT extends IAnyType> = T extends object ? T & IStateTreeNode<IT> : T;
7/** @hidden */
8declare const $type: unique symbol;
9/**
10 * A type, either complex or simple.
11 */
12export interface IType<C, S, T> {
13 /** @hidden */
14 readonly [$type]: undefined;
15 /**
16 * Friendly type name.
17 */
18 name: string;
19 /**
20 * Name of the identifier attribute or null if none.
21 */
22 readonly identifierAttribute?: string;
23 /**
24 * Creates an instance for the type given an snapshot input.
25 *
26 * @returns An instance of that type.
27 */
28 create(snapshot?: C, env?: any): this["Type"];
29 /**
30 * Checks if a given snapshot / instance is of the given type.
31 *
32 * @param thing Snapshot or instance to be checked.
33 * @returns true if the value is of the current type, false otherwise.
34 */
35 is(thing: any): thing is C | this["Type"];
36 /**
37 * Run's the type's typechecker on the given value with the given validation context.
38 *
39 * @param thing Value to be checked, either a snapshot or an instance.
40 * @param context Validation context, an array of { subpaths, subtypes } that should be validated
41 * @returns The validation result, an array with the list of validation errors.
42 */
43 validate(thing: C, context: IValidationContext): IValidationResult;
44 /**
45 * Gets the textual representation of the type as a string.
46 */
47 describe(): string;
48 /**
49 * @deprecated use `Instance<typeof MyType>` instead.
50 * @hidden
51 */
52 readonly Type: STNValue<T, this>;
53 /**
54 * @deprecated do not use.
55 * @hidden
56 */
57 readonly TypeWithoutSTN: T;
58 /**
59 * @deprecated use `SnapshotOut<typeof MyType>` instead.
60 * @hidden
61 */
62 readonly SnapshotType: S;
63 /**
64 * @deprecated use `SnapshotIn<typeof MyType>` instead.
65 * @hidden
66 */
67 readonly CreationType: C;
68}
69/**
70 * Any kind of type.
71 */
72export interface IAnyType extends IType<any, any, any> {
73}
74/**
75 * A simple type, this is, a type where the instance and the snapshot representation are the same.
76 */
77export interface ISimpleType<T> extends IType<T, T, T> {
78}
79/** @hidden */
80export declare type Primitives = ModelPrimitive | null | undefined;
81/**
82 * A complex type.
83 * @deprecated just for compatibility with old versions, could be deprecated on the next major version
84 * @hidden
85 */
86export interface IComplexType<C, S, T> extends IType<C, S, T & object> {
87}
88/**
89 * Any kind of complex type.
90 */
91export interface IAnyComplexType extends IType<any, any, object> {
92}
93/** @hidden */
94export declare type ExtractCSTWithoutSTN<IT extends {
95 [$type]: undefined;
96 CreationType: any;
97 SnapshotType: any;
98 TypeWithoutSTN: any;
99}> = IT["CreationType"] | IT["SnapshotType"] | IT["TypeWithoutSTN"];
100/** @hidden */
101export declare type ExtractCSTWithSTN<IT extends {
102 [$type]: undefined;
103 CreationType: any;
104 SnapshotType: any;
105 Type: any;
106}> = IT["CreationType"] | IT["SnapshotType"] | IT["Type"];
107/**
108 * The instance representation of a given type.
109 */
110export declare type Instance<T> = T extends {
111 [$type]: undefined;
112 Type: any;
113} ? T["Type"] : T;
114/**
115 * The input (creation) snapshot representation of a given type.
116 */
117export declare type SnapshotIn<T> = T extends {
118 [$type]: undefined;
119 CreationType: any;
120} ? T["CreationType"] : T extends IStateTreeNode<infer IT> ? IT["CreationType"] : T;
121/**
122 * The output snapshot representation of a given type.
123 */
124export declare type SnapshotOut<T> = T extends {
125 [$type]: undefined;
126 SnapshotType: any;
127} ? T["SnapshotType"] : T extends IStateTreeNode<infer IT> ? IT["SnapshotType"] : T;
128/**
129 * A type which is equivalent to the union of SnapshotIn and Instance types of a given typeof TYPE or typeof VARIABLE.
130 * For primitives it defaults to the primitive itself.
131 *
132 * For example:
133 * - `SnapshotOrInstance<typeof ModelA> = SnapshotIn<typeof ModelA> | Instance<typeof ModelA>`
134 * - `SnapshotOrInstance<typeof self.a (where self.a is a ModelA)> = SnapshotIn<typeof ModelA> | Instance<typeof ModelA>`
135 *
136 * Usually you might want to use this when your model has a setter action that sets a property.
137 *
138 * Example:
139 * ```ts
140 * const ModelA = types.model({
141 * n: types.number
142 * })
143 *
144 * const ModelB = types.model({
145 * innerModel: ModelA
146 * }).actions(self => ({
147 * // this will accept as property both the snapshot and the instance, whichever is preferred
148 * setInnerModel(m: SnapshotOrInstance<typeof self.innerModel>) {
149 * self.innerModel = cast(m)
150 * }
151 * }))
152 * ```
153 */
154export declare type SnapshotOrInstance<T> = SnapshotIn<T> | Instance<T>;
155/**
156 * Returns if a given value represents a type.
157 *
158 * @param value Value to check.
159 * @returns `true` if the value is a type.
160 */
161export declare function isType(value: any): value is IAnyType;
162export {};