forked from crissdev/knockout.mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknockout.mapping.d.ts
More file actions
261 lines (230 loc) · 11.1 KB
/
Copy pathknockout.mapping.d.ts
File metadata and controls
261 lines (230 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as ko from "knockout";
declare module "knockout" {
export module mapping {
export type MappedObservable<T> = {
[P in keyof T]:
T[P] extends ko.Observable<infer E> | ko.ObservableArray<infer E> ? T[P] :
T[P] extends string | boolean | number | Date ? ko.Observable<T[P]> :
T[P] extends Array<infer E> ? ko.ObservableArray<MappedObservable<E>> :
T[P] extends Function ? T[P] :
T[P] extends object ? MappedObservable<T[P]> :
T[P];
};
export type MappingOptions<T = any> = MappingOptionsBase<T> & MappingOptionsSpecific<T>;
export interface MappingOptionsBase<T> {
ignore?: (keyof T)[];
include?: (keyof T)[];
copy?: (keyof T)[];
observe?: (keyof T)[];
mappedProperties?: (keyof T)[];
deferEvaluation?: boolean;
}
export interface MappingOptionsProperty<T> extends MappingOptionsBase<T> {
create?: (options: CreateOptions<T>) => void;
update?: (options: UpdateOptions<T>) => void;
key?: (data: T) => any;
}
export type MappingOptionsSpecific<T> = {
[P in keyof T]?:
T[P] extends Array<infer U> ? MappingOptionsProperty<U> :
MappingOptionsProperty<T[P]>;
};
export interface CreateOptions<T> {
data: T;
parent: any;
}
export interface UpdateOptions<T> {
data: T;
parent: any;
target: any;
observable?: ko.Observable<any>;
}
export interface VisitModelOptions {
visitedObjects?: any;
parentName?: string;
ignore?: string[];
copy?: string[];
include?: string[];
}
/**
* Checks if an object was created using `knockout.mapping`.
* @param viewModel View model object to be checked.
*/
export function isMapped(viewModel: any): boolean;
/**
* Updates target observable with value from the source.
*
* @param source Plain JavaScript value to be mapped.
* @param target Observable to be updated.
*/
export function fromJS(source: string, target: ko.Observable<string>): ko.Observable<string>;
/**
* Creates an observable wrapping source's value.
* If 'target' is supplied, instead, target observable is updated.
*
* @param source Plain JavaScript value to be mapped.
* @param options The mapping options.
* @param target Observable to be updated.
*/
export function fromJS(source: string, inputOptions?: MappingOptions<string>, target?: ko.Observable<string>): ko.Observable<string>;
/**
* Updates target observable with value from the source.
*
* @param source Plain JavaScript value to be mapped.
* @param target Observable to be updated.
*/
export function fromJS(source: number, target: ko.Observable<number>): ko.Observable<number>;
/**
* Creates an observable wrapping source's value.
* If 'target' is supplied, instead, target observable is updated.
*
* @param source Plain JavaScript value to be mapped.
* @param options The mapping options.
* @param target Observable to be updated.
*/
export function fromJS(source: number, inputOptions?: MappingOptions<number>, target?: ko.Observable<number>): ko.Observable<number>;
/**
* Updates target observable with value from the source.
*
* @param source Plain JavaScript value to be mapped.
* @param target Observable to be updated.
*/
export function fromJS(source: boolean, target: ko.Observable<boolean>): ko.Observable<boolean>;
/**
* Creates an observable wrapping source's value.
* If 'target' is supplied, instead, target observable is updated.
*
* @param source Plain JavaScript value to be mapped.
* @param options The mapping options.
* @param target Observable to be updated.
*/
export function fromJS(source: boolean, inputOptions?: MappingOptions<boolean>, target?: ko.Observable<boolean>): ko.Observable<boolean>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
*
* @param source Plain JavaScript array to be mapped.
*/
export function fromJS<SourceT = any>(source: SourceT[]): ko.ObservableArray<MappedObservable<SourceT>>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
*
* @param source Plain JavaScript array to be mapped.
* @param inputOptions The mappings options with no properties.
*/
export function fromJS<SourceT = any>(source: SourceT[], inputOptions: {}): ko.ObservableArray<MappedObservable<SourceT>>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
* If 'target' is supplied, instead, target's observable properties are updated.
*
* @param source Plain JavaScript array to be mapped.
* @param options The mapping options.
* @param target View model object previously mapped to be updated.
*/
export function fromJS<MappedT = any, SourceT = any>(source: SourceT[], inputOptions: MappingOptions<SourceT>, target?: ko.ObservableArray<MappedT>): ko.ObservableArray<MappedT>;
/**
* Updates target's observable properties with those of the sources.
*
* @param source Plain JavaScript array to be mapped.
* @param target View model object previously mapped to be updated.
*/
export function fromJS<MappedT = any, SourceT = any>(source: SourceT[], target: ko.ObservableArray<MappedT>): ko.ObservableArray<MappedT>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
*
* @param source Plain JavaScript object to be mapped.
*/
export function fromJS<SourceT = any>(source: SourceT): MappedObservable<SourceT>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
*
* @param source Plain JavaScript object to be mapped.
* @param inputOptions The mappings options with no properties.
*/
export function fromJS<SourceT = any>(source: SourceT, inputOptions: {}): MappedObservable<SourceT>;
/**
* Creates a view model object with observable properties for each of the properties on the source.
* If 'target' is supplied, instead, target's observable properties are updated.
*
* @param source Plain JavaScript object to be mapped.
* @param options The mapping options.
* @param target View model object previously mapped to be updated.
*/
export function fromJS<MappedT = any, SourceT = any>(source: SourceT, inputOptions: MappingOptions<SourceT>, target?: MappedT): MappedT;
/**
* Updates target's observable properties with those of the sources.
*
* @param source Plain JavaScript object to be mapped.
* @param target View model object previously mapped to be updated.
*/
export function fromJS<MappedT = any, SourceT = any>(source: SourceT, target: MappedT): MappedT;
/**
* Creates a view model object with observable properties for each of the properties on the source.
* If 'target' is supplied, instead, target's observable properties are updated.
*
* @param jsonString JSON of a JavaScript object to be mapped.
* @param options Options on mapping behavior.
* @param target View model object previosly mapped to be updated.
*/
export function fromJSON<MappedT = any, SourceT = any>(jsonString: string, inputOptions?: MappingOptions<SourceT>, target?: MappedT): MappedT;
/**
* Updates target's observable properties with those of the sources.
*
* @param jsonString JSON of a JavaScript object to be mapped.
* @param target View model object previously mapped to be updated.
*/
export function fromJSON<MappedT = any, SourceT = any>(jsonString: string, target: MappedT): MappedT;
/**
* Creates an unmapped object containing only the properties of the mapped object that were part of your original JS object.
*
* @param rootObject Object with observables to be converted.
* @param options The mapping options
*/
export function toJS<SourceT = any, MappedT = any>(rootObject: MappedT, options?: MappingOptions<SourceT>): SourceT;
/**
* Creates an unmapped object containing only the properties of the mapped object that were part of your original JS object.
* Stringify the result.
*
* @param rootObject Object with observables to be converted.
* @param options The mapping options.
* @param replacer Same as JSON.stringify
* @param space Sam as JSON.stringify
*/
export function toJSON<SourceT = any>(rootObject: SourceT, options?: MappingOptions<SourceT>, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
/** Get the default mapping options. */
export function defaultOptions(): MappingOptions;
/**
* Sets the default mapping options.
*
* @param options The new default options.
*/
export function defaultOptions(options: MappingOptions): void;
/** Undocumented. Reset Mapping default options to the original ones. */
export function resetDefaultOptions(): void;
/**
* Undocumented. Custom implementation of JavaScript's typeof.
*
* @param x Object to check type.
*/
export function getType(x: any): string;
/**
* Undocumented. Visit an object and executes callback on each properties.
*
* @param rootObject The root object to visit.
* @param callback The callback which is executed on each properties.
* @param options The options for the visiting.
*/
export function visitModel<T = any>(rootObject: Object, callback: (propertyValue: any, parentName: string) => any, options?: VisitModelOptions): T;
}
export interface ObservableArrayFunctions<T> {
mappedCreate(item: T): T;
mappedRemove(item: T): T[];
mappedRemove(removeFunction: (item: T) => boolean): T[];
mappedRemoveAll(): T[];
mappedRemoveAll(items: T[]): T[];
mappedDestroy(item: T): void;
mappedDestroy(destroyFunction: (item: T) => boolean): void;
mappedDestroyAll(): void;
mappedDestroyAll(items: T[]): void;
}
}
export = ko.mapping;