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