diff --git a/global.d.ts b/global.d.ts index 197d0016..766151be 100644 --- a/global.d.ts +++ b/global.d.ts @@ -87,14 +87,15 @@ declare global { //#region subscribables/dependencyDetection.js - export interface ComputedContext { - getDependenciesCount(): number; - getDependencies(): Subscribable[]; - isInitial(): boolean; - registerDependency(subscribable: Subscribable): void; - } + // transfered types to dependencyDetection.ts + // export interface ComputedContext { + // getDependenciesCount(): number; + // getDependencies(): Subscribable[]; + // isInitial(): boolean; + // registerDependency(subscribable: Subscribable): void; + // } - export const computedContext: ComputedContext; + // export const computedContext: ComputedContext; /** * Executes a function and returns the result, while disabling depdendency tracking @@ -102,75 +103,80 @@ declare global { * @param callbackTarget - the `this` binding for `callback` * @param callbackArgs - the args to provide to `callback` */ - export function ignoreDependencies( - callback: (this: Target, ...args: Args) => Return, - callbackTarget?: Target, - callbackArgs?: Args - ): Return; + // types transfered to dependencyDetection.ts + // export function ignoreDependencies( + // callback: (this: Target, ...args: Args) => Return, + // callbackTarget?: Target, + // callbackArgs?: Args + // ): Return; //#endregion //#region subscribables/extenders.js - export type RateLimitMethod = (callback: () => void, timeout: number, options: any) => (() => void); + // no usage anymore + // export type RateLimitMethod = (callback: () => void, timeout: number, options: any) => (() => void); - export interface RateLimitOptions { - timeout: number; - method?: "notifyAtFixedRate" | "notifyWhenChangesStop" | RateLimitMethod; - [option: string]: any; - } + // export interface RateLimitOptions { + // timeout: number; + // method?: "notifyAtFixedRate" | "notifyWhenChangesStop" | RateLimitMethod; + // [option: string]: any; + // } - export interface ExtendersOptions { - trackArrayChanges: true | utils.CompareArraysOptions; - throttle: number; - rateLimit: number | RateLimitOptions; - deferred: true; - notify: "always" | any; - } + // types transfered to extenders.ts and throttleExpanders.ts + // export interface ExtendersOptions { + // trackArrayChanges: true | utils.CompareArraysOptions; + // throttle: number; + // rateLimit: number | RateLimitOptions; + // deferred: true; + // notify: "always" | any; + // } - export interface Extender { - (target: T, options: O): T; - } + // export interface Extender { + // (target: T, options: O): T; + // } - type AsExtenders = { [P in keyof T]: Extender } + // type AsExtenders = { [P in keyof T]: Extender } - export interface Extenders extends AsExtenders> { - [name: string]: Extender; - } + // export interface Extenders extends AsExtenders> { + // [name: string]: Extender; + // } - export interface ObservableExtenderOptions extends Partial> { } + // export interface ObservableExtenderOptions extends Partial> { } - export const extenders: Extenders; + // export const extenders: Extenders; //#endregion //#region subscribables/mappingHelpers.js - export type Unwrapped = T extends ko.ObservableArray - ? Unwrapped[] - : T extends ko.Subscribable - ? ( - R extends ko.Subscribable - ? unknown - : R extends Record - ? { [P in keyof R]: Unwrapped } - : R - ) - : T extends Date | RegExp | Function - ? T - : T extends Record - ? { [P in keyof T]: Unwrapped } - : T - - export function toJS(rootObject: T): Unwrapped; - export function toJSON(rootObject: any, replacer?: Function, space?: number): string; + // transfered to mappingHelpers.ts + // export type Unwrapped = T extends ko.ObservableArray + // ? Unwrapped[] + // : T extends ko.Subscribable + // ? ( + // R extends ko.Subscribable + // ? unknown + // : R extends Record + // ? { [P in keyof R]: Unwrapped } + // : R + // ) + // : T extends Date | RegExp | Function + // ? T + // : T extends Record + // ? { [P in keyof T]: Unwrapped } + // : T + + // export function toJS(rootObject: T): Unwrapped; + // export function toJSON(rootObject: any, replacer?: Function, space?: number): string; //#endregion //#region subscribables/observableUtils.js - export function when(predicate: ComputedReadFunction, callback: SubscriptionCallback, context?: TTarget): Subscription; - export function when(predicate: ComputedReadFunction): Promise; + // types to subscribable + // export function when(predicate: ComputedReadFunction, callback: SubscriptionCallback, context?: TTarget): Subscription; + // export function when(predicate: ComputedReadFunction): Promise; //#endregion @@ -178,6 +184,7 @@ declare global { export type BindingAccessors = { [name: string]: Function; }; + // usage in applyBindings, BindingHandler, event, checked, options export interface AllBindings { (): any; @@ -186,8 +193,10 @@ declare global { has(name: string): boolean; } - export type BindingHandlerControlsDescendant = { controlsDescendantBindings: boolean; } - export type BindingHandlerAddBinding = (name: string, value: any) => void; + // transfered to LegacyBindingHandler.ts + // export type BindingHandlerControlsDescendant = { controlsDescendantBindings: boolean; } + // export type BindingHandlerAddBinding = (name: string, value: any) => void; + // used as Base for all BindingHandlers export interface BindingHandler { after?: string[]; init?: (element: any, valueAccessor: () => T, allBindings: AllBindings, viewModel: any, bindingContext: BindingContext) => void | BindingHandlerControlsDescendant; @@ -200,6 +209,7 @@ declare global { [name: string]: BindingHandler; } + // global usage. defined in bindingContext.ts export interface BindingContext { ko: any; // typeof ko; diff --git a/packages/bind/src/BindingHandler.ts b/packages/bind/src/BindingHandler.ts index c9768caa..4ef98be1 100644 --- a/packages/bind/src/BindingHandler.ts +++ b/packages/bind/src/BindingHandler.ts @@ -4,13 +4,13 @@ import { isWriteableObservable } from '@tko/observable' import { LifeCycle } from '@tko/lifecycle' export class BindingHandler extends LifeCycle { - $context: any + $context: any // most likly BindingContext but params must be typed first $element: HTMLElement $data: any bindingCompletion: any valueAccessor: Function completeBinding: any - allBindings: any + allBindings: AllBindings constructor (params) { super() diff --git a/packages/bind/src/DescendantBindingHandler.ts b/packages/bind/src/DescendantBindingHandler.ts index 00e229bf..47126a45 100644 --- a/packages/bind/src/DescendantBindingHandler.ts +++ b/packages/bind/src/DescendantBindingHandler.ts @@ -11,7 +11,7 @@ import { AsyncBindingHandler } from './BindingHandler' export class DescendantBindingHandler extends AsyncBindingHandler { get controlsDescendants () { return true } - async applyBindingsToDescendants (childContext: any, callback?: Function) { + async applyBindingsToDescendants (childContext: BindingContext, callback?: Function) { const bindingResult = applyBindingsToDescendants(childContext, this.$element) if (bindingResult.isSync) { this.bindingCompletion = bindingResult diff --git a/packages/bind/src/LegacyBindingHandler.ts b/packages/bind/src/LegacyBindingHandler.ts index 8e94ba3d..6867fa34 100644 --- a/packages/bind/src/LegacyBindingHandler.ts +++ b/packages/bind/src/LegacyBindingHandler.ts @@ -56,7 +56,7 @@ export class LegacyBindingHandler extends BindingHandler { ] } - get controlsDescendants(): any { + get controlsDescendants(): boolean { const objectToTest = this.initReturn || this.handler || {} return objectToTest.controlsDescendantBindings } diff --git a/packages/bind/src/applyBindings.ts b/packages/bind/src/applyBindings.ts index ca50608f..81154f69 100644 --- a/packages/bind/src/applyBindings.ts +++ b/packages/bind/src/applyBindings.ts @@ -34,9 +34,9 @@ interface BindingError { during: string, errorCaptured: any, bindings?: any, - allBindings?: any, + allBindings?: AllBindings, bindingKey?: string, - bindingContext: any, + bindingContext: BindingContext, element: HTMLElement, valueAccessor?: Function, message?: string, @@ -263,7 +263,7 @@ function applyBindingsToNodeInternal (node: HTMLElement, sourceBindings: any, bi } : (bindingKey) => bindings[bindingKey] // Use of allBindings as a function is maintained for backwards compatibility, but its use is deprecated - const allBindings = function () { + const allBindings: AllBindings = function () { return objectMap(bindingsUpdater ? bindingsUpdater() : bindings, evaluateValueAccessor) } diff --git a/packages/binding.component/src/componentBinding.ts b/packages/binding.component/src/componentBinding.ts index 05d7b8e6..264f8f0f 100644 --- a/packages/binding.component/src/componentBinding.ts +++ b/packages/binding.component/src/componentBinding.ts @@ -29,7 +29,7 @@ import registry from '@tko/utils.component' var componentLoadingOperationUniqueId = 0 export default class ComponentBinding extends DescendantBindingHandler { - childBindingContext: any; + childBindingContext: BindingContext; currentLoadingOperationId: number | null; currentViewModel: any; latestComponentName: string; diff --git a/packages/binding.core/src/attr.ts b/packages/binding.core/src/attr.ts index c5302654..c6bbe766 100644 --- a/packages/binding.core/src/attr.ts +++ b/packages/binding.core/src/attr.ts @@ -8,7 +8,7 @@ import { } from '@tko/observable' export var attr = { - update: function (element, valueAccessor, allBindings) { + update: function (element, valueAccessor, allBindings: AllBindings) { // allBindings not used! var value = unwrap(valueAccessor()) || {} objectForEach(value, function (attrName, attrValue) { attrValue = unwrap(attrValue) diff --git a/packages/binding.core/src/checked.ts b/packages/binding.core/src/checked.ts index 489926f6..b3dcedc3 100644 --- a/packages/binding.core/src/checked.ts +++ b/packages/binding.core/src/checked.ts @@ -13,7 +13,7 @@ import { export var checked = { after: ['value', 'attr'], - init: function (element, valueAccessor, allBindings) { + init: function (element, valueAccessor, allBindings: AllBindings) { var checkedValue = pureComputed(function () { // Treat "value" like "checkedValue" when it is included with "checked" binding if (allBindings.has('checkedValue')) { diff --git a/packages/binding.core/src/event.ts b/packages/binding.core/src/event.ts index 9bf1ef58..baa09709 100644 --- a/packages/binding.core/src/event.ts +++ b/packages/binding.core/src/event.ts @@ -12,7 +12,7 @@ import { // e.g. click:handler instead of the usual full-length event:{click:handler} export function makeEventHandlerShortcut (eventName) { return { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + init: function (element, valueAccessor, allBindings: AllBindings, viewModel, bindingContext: BindingContext) { var newValueAccessor = function () { var result = {} result[eventName] = valueAccessor() @@ -28,7 +28,7 @@ function makeDescriptor (handlerOrObject) { } export const eventHandler = { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) { var eventsToHandle = valueAccessor() || {} objectForEach(eventsToHandle, function (eventName, descriptor) { const {passive, capture, once, debounce, throttle} = makeDescriptor(descriptor) diff --git a/packages/binding.core/src/let.ts b/packages/binding.core/src/let.ts index e7f15765..cdd28b06 100644 --- a/packages/binding.core/src/let.ts +++ b/packages/binding.core/src/let.ts @@ -4,7 +4,7 @@ import { } from '@tko/bind' export default { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel not used! // Make a modified binding context, with extra properties, and apply it to descendant elements var innerContext = bindingContext['extend'](valueAccessor) applyBindingsToDescendants(innerContext, element) diff --git a/packages/binding.core/src/options.ts b/packages/binding.core/src/options.ts index ef22d697..579f082d 100644 --- a/packages/binding.core/src/options.ts +++ b/packages/binding.core/src/options.ts @@ -28,7 +28,7 @@ export var options = { // Ensures that the binding processor doesn't try to bind the options return { 'controlsDescendantBindings': true } }, - update: function (element, valueAccessor, allBindings) { + update: function (element, valueAccessor, allBindings: AllBindings) { function selectedOptions () { return arrayFilter(element.options, function (node) { return node.selected }) } diff --git a/packages/binding.core/src/selectedOptions.ts b/packages/binding.core/src/selectedOptions.ts index 6d61b60c..00fb1d74 100644 --- a/packages/binding.core/src/selectedOptions.ts +++ b/packages/binding.core/src/selectedOptions.ts @@ -11,7 +11,7 @@ import { export var selectedOptions = { after: ['options', 'foreach'], - init: function (element, valueAccessor, allBindings) { + init: function (element, valueAccessor, allBindings: AllBindings) { // allBindings not in use registerEventHandler(element, 'change', function () { var value = valueAccessor(), valueToWrite = new Array() arrayForEach(element.getElementsByTagName('option'), function (node) { diff --git a/packages/binding.core/src/submit.ts b/packages/binding.core/src/submit.ts index 2773743d..6cf5d88a 100644 --- a/packages/binding.core/src/submit.ts +++ b/packages/binding.core/src/submit.ts @@ -4,7 +4,7 @@ import { } from '@tko/utils' export var submit = { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel not in use if (typeof valueAccessor() !== 'function') { throw new Error('The value for a submit binding must be a function') } registerEventHandler(element, 'submit', function (event) { var handlerReturnValue diff --git a/packages/binding.core/src/using.ts b/packages/binding.core/src/using.ts index 9d604c0a..3a1d7319 100644 --- a/packages/binding.core/src/using.ts +++ b/packages/binding.core/src/using.ts @@ -3,7 +3,7 @@ import { } from '@tko/bind' export var using = { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + init: function (element, valueAccessor, allBindings: AllBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel aren't actually used here var innerContext = bindingContext.createChildContext(valueAccessor) applyBindingsToDescendants(innerContext, element) return { controlsDescendantBindings: true } diff --git a/packages/binding.foreach/src/foreach.ts b/packages/binding.foreach/src/foreach.ts index 5989585e..2c5f3663 100644 --- a/packages/binding.foreach/src/foreach.ts +++ b/packages/binding.foreach/src/foreach.ts @@ -75,7 +75,7 @@ export class ForEachBinding extends AsyncBindingHandler { // computed // {data: array, name: string, as: string} afterAdd; - allBindings; + allBindings: AllBindings; static animateFrame; as; beforeRemove; diff --git a/packages/binding.template/helpers/dummyTemplateEngine.ts b/packages/binding.template/helpers/dummyTemplateEngine.ts index b0888840..60dbac78 100644 --- a/packages/binding.template/helpers/dummyTemplateEngine.ts +++ b/packages/binding.template/helpers/dummyTemplateEngine.ts @@ -37,7 +37,7 @@ export function dummyTemplateEngine(templates?) { return new anonymousTemplate(template); // Anonymous template }; - this.renderTemplateSource = function (templateSource, bindingContext, rt_options, templateDocument) { + this.renderTemplateSource = function (templateSource, bindingContext: BindingContext, rt_options, templateDocument) { var data = bindingContext['$data']; if (data && typeof data.get_value === 'function') { // For cases when data is an Identifier/Expression. diff --git a/packages/binding.template/src/nativeTemplateEngine.ts b/packages/binding.template/src/nativeTemplateEngine.ts index 77a02a51..e0f9b171 100644 --- a/packages/binding.template/src/nativeTemplateEngine.ts +++ b/packages/binding.template/src/nativeTemplateEngine.ts @@ -16,7 +16,7 @@ export function nativeTemplateEngine () { nativeTemplateEngine.prototype = new templateEngine() nativeTemplateEngine.prototype.constructor = nativeTemplateEngine -nativeTemplateEngine.prototype.renderTemplateSource = function (templateSource, bindingContext, options, templateDocument) { +nativeTemplateEngine.prototype.renderTemplateSource = function (templateSource, bindingContext: BindingContext, options, templateDocument) { let version: number; if (ieVersion instanceof Array) { version = parseInt(ieVersion[1], 10); diff --git a/packages/binding.template/src/templateEngine.ts b/packages/binding.template/src/templateEngine.ts index 6d89dbb3..39975276 100644 --- a/packages/binding.template/src/templateEngine.ts +++ b/packages/binding.template/src/templateEngine.ts @@ -36,7 +36,7 @@ import { export function templateEngine () { }; extend(templateEngine.prototype, { - renderTemplateSource: function (templateSource, bindingContext, options, templateDocument) { + renderTemplateSource: function (templateSource, bindingContext: BindingContext, options, templateDocument) { // templateSource, bindingContext, templateDocument not in use options.onError('Override renderTemplateSource') }, @@ -57,7 +57,7 @@ extend(templateEngine.prototype, { } else { options.onError(new Error('Unknown template type: ' + template)) } }, - renderTemplate: function (template, bindingContext, options, templateDocument) { + renderTemplate: function (template, bindingContext: BindingContext, options, templateDocument) { var templateSource = this['makeTemplateSource'](template, templateDocument) return this.renderTemplateSource(templateSource, bindingContext, options, templateDocument) } diff --git a/packages/binding.template/src/templating.ts b/packages/binding.template/src/templating.ts index 0fc1d162..654a70e2 100644 --- a/packages/binding.template/src/templating.ts +++ b/packages/binding.template/src/templating.ts @@ -46,7 +46,7 @@ function invokeForEachNodeInContinuousRange (firstNode, lastNode, action) { } } -function activateBindingsOnContinuousNodeArray (continuousNodeArray, bindingContext, afterBindingCallback) { +function activateBindingsOnContinuousNodeArray (continuousNodeArray, bindingContext: BindingContext, afterBindingCallback) { // To be used on any nodes that have been rendered by a template and have been inserted into some parent element // Walks through continuousNodeArray (which *must* be continuous, i.e., an uninterrupted sequence of sibling nodes, because // the algorithm for walking them relies on this), and for each top-level item in the virtual-element sense, diff --git a/packages/computed/src/throttleExtender.ts b/packages/computed/src/throttleExtender.ts index 587e4f57..a076469a 100644 --- a/packages/computed/src/throttleExtender.ts +++ b/packages/computed/src/throttleExtender.ts @@ -1,14 +1,12 @@ import { extenders as baseExtenders } from '@tko/observable' import { computed } from './computed' +import { BaseExtendersType } from 'packages/observable/src/extenders' -interface ExtendersType{ - notify (target, notifyWhen) - deferred (target, option) - rateLimit (target, options) - throttle (target, timout) +interface ExtendersType extends BaseExtendersType { + throttle(target: any, timout: number): void } -export function throttleExtender (target, timeout) { +export function throttleExtender (target: any, timeout: number) { // Throttling means two things: // (1) For dependent observables, we throttle *evaluations* so that, no matter how fast its dependencies @@ -29,6 +27,6 @@ export function throttleExtender (target, timeout) { }) } -const extenders = baseExtenders as ExtendersType; +const extenders = baseExtenders as BaseExtendersType; -extenders.throttle = throttleExtender +(extenders as ExtendersType).throttle = throttleExtender diff --git a/packages/observable/src/defer.ts b/packages/observable/src/defer.ts index 5a686957..37339c1f 100644 --- a/packages/observable/src/defer.ts +++ b/packages/observable/src/defer.ts @@ -4,7 +4,7 @@ // import { tasks } from '@tko/utils' -export function deferUpdates (target) { +export function deferUpdates(target: any) { if (target._deferUpdates) { return } target._deferUpdates = true target.limit(function (callback) { diff --git a/packages/observable/src/dependencyDetection.ts b/packages/observable/src/dependencyDetection.ts index 867bd174..7a9b313e 100644 --- a/packages/observable/src/dependencyDetection.ts +++ b/packages/observable/src/dependencyDetection.ts @@ -4,6 +4,7 @@ // // In KO 3.x, dependencyDetection was also known as computedContext. // +import { Func } from 'mocha' import { isSubscribable } from './subscribableSymbol' const outerFrames = new Array() @@ -29,14 +30,17 @@ export function end () { currentFrame = outerFrames.pop() } -export function registerDependency (subscribable) { +export function registerDependency (subscribable): void { if (currentFrame) { if (!isSubscribable(subscribable)) { throw new Error('Only subscribable things can act as dependencies') } currentFrame.callback.call(currentFrame.callbackTarget, subscribable, subscribable._id || (subscribable._id = getId())) } } -export function ignore (callback, callbackTarget?, callbackArgs?) { +export function ignore( + callback: Function, + callbackTarget?: any, + callbackArgs?: any[]): any { try { begin() return callback.apply(callbackTarget, callbackArgs || []) @@ -45,16 +49,22 @@ export function ignore (callback, callbackTarget?, callbackArgs?) { } } -export function getDependenciesCount () { - if (currentFrame) { return currentFrame.computed.getDependenciesCount() } +export function getDependenciesCount(): number | undefined { + if (currentFrame) { + return currentFrame.computed.getDependenciesCount(); + } } -export function getDependencies () { - if (currentFrame) { return currentFrame.computed.getDependencies() } +export function getDependencies(): Subscribable[] | undefined{ + if (currentFrame) { + return currentFrame.computed.getDependencies(); + } } -export function isInitial () { - if (currentFrame) { return currentFrame.isInitial } +export function isInitial(): boolean | undefined { + if (currentFrame) { + return currentFrame.isInitial; + } } export { ignore as ignoreDependencies } diff --git a/packages/observable/src/extenders.ts b/packages/observable/src/extenders.ts index 508afa19..06c9fd35 100644 --- a/packages/observable/src/extenders.ts +++ b/packages/observable/src/extenders.ts @@ -38,20 +38,20 @@ export function applyExtenders (requestedExtenders?) { */ // Change when notifications are published. -export function notify (target, notifyWhen) { +export function notify(target: any, notifyWhen: string) { target.equalityComparer = notifyWhen == 'always' ? null // null equalityComparer means to always notify : valuesArePrimitiveAndEqual } -export function deferred (target, option) { +export function deferred(target: any, option: boolean) { if (option !== true) { throw new Error('The \'deferred\' extender only accepts the value \'true\', because it is not supported to turn deferral off once enabled.') } deferUpdates(target) } -export function rateLimit (target, options) { +export function rateLimit(target: any, options: string | any) { var timeout, method, limitFunction if (typeof options === 'number') { @@ -61,7 +61,7 @@ export function rateLimit (target, options) { method = options.method } - // rateLimit supersedes deferred updates + // rateLimit supersedes deferred updates target._deferUpdates = false limitFunction = method === 'notifyWhenChangesStop' ? debounceFn : throttleFn @@ -71,13 +71,13 @@ export function rateLimit (target, options) { }) } -interface ExtendersType{ - notify (target, notifyWhen) - deferred (target, option) - rateLimit (target, options) +export interface BaseExtendersType{ + notify(target: any, notifyWhen: string): void, + deferred(target: any, option: boolean): void, + rateLimit(target: any, options: string | any): void } -export var extenders: ExtendersType = { +export var extenders: BaseExtendersType = { notify: notify, deferred: deferred, rateLimit: rateLimit diff --git a/packages/observable/src/mappingHelpers.ts b/packages/observable/src/mappingHelpers.ts index 78a78e69..727934a7 100644 --- a/packages/observable/src/mappingHelpers.ts +++ b/packages/observable/src/mappingHelpers.ts @@ -6,33 +6,37 @@ import { isObservable } from './observable' -var maxNestedObservableDepth = 10 // Escape the (unlikely) pathological case where an observable's current value is itself (or similar reference cycle) +var maxNestedObservableDepth: number = 10 // Escape the (unlikely) pathological case where an observable's current value is itself (or similar reference cycle) -export function toJS (rootObject) { - if (arguments.length == 0) { throw new Error('When calling ko.toJS, pass the object you want to convert.') } +export function toJS(rootObject: T): T { + if (arguments.length == 0) { + throw new Error('When calling ko.toJS, pass the object you want to convert.'); + } // We just unwrap everything at every level in the object graph - return mapJsObjectGraph(rootObject, function (valueToMap) { + return mapJsObjectGraph(rootObject, function (valueToMap: any) { // Loop because an observable's value might in turn be another observable wrapper for (var i = 0; isObservable(valueToMap) && (i < maxNestedObservableDepth); i++) { valueToMap = valueToMap() } return valueToMap }) } -export function toJSON (rootObject, replacer, space) { // replacer and space are optional +export function toJSON(rootObject: T, replacer?: (key: string, value: any) => any, space?: string | number): string { // replacer and space are optional var plainJavaScriptObject = toJS(rootObject) return JSON.stringify(plainJavaScriptObject, replacer, space) } -function mapJsObjectGraph (rootObject, mapInputCallback, visitedObjects = new Map()) { +function mapJsObjectGraph(rootObject: T, mapInputCallback: (value: any) => any, visitedObjects = new Map()): any { rootObject = mapInputCallback(rootObject) var canHaveProperties = (typeof rootObject === 'object') && (rootObject !== null) && (rootObject !== undefined) && (!(rootObject instanceof RegExp)) && (!(rootObject instanceof Date)) && (!(rootObject instanceof String)) && (!(rootObject instanceof Number)) && (!(rootObject instanceof Boolean)) - if (!canHaveProperties) { return rootObject } + if (!canHaveProperties) { + return rootObject; + } - var outputProperties = rootObject instanceof Array ? [] : {} + var outputProperties: any = rootObject instanceof Array ? [] : {} visitedObjects.set(rootObject, outputProperties) - visitPropertiesOrArrayEntries(rootObject, function (indexer) { + visitPropertiesOrArrayEntries(rootObject, function (indexer: any) { var propertyValue = mapInputCallback(rootObject[indexer]) switch (typeof propertyValue) { @@ -55,12 +59,14 @@ function mapJsObjectGraph (rootObject, mapInputCallback, visitedObjects = new Ma return outputProperties } -function visitPropertiesOrArrayEntries (rootObject, visitorCallback) { +function visitPropertiesOrArrayEntries(rootObject: T, visitorCallback: (indexer: any) => void): void { if (rootObject instanceof Array) { - for (var i = 0; i < rootObject.length; i++) { visitorCallback(i) } + for (var i = 0; i < rootObject.length; i++) { + visitorCallback(i) + } // For arrays, also respect toJSON property for custom mappings (fixes #278) - if (typeof rootObject['toJSON'] === 'function') { visitorCallback('toJSON') } + if (typeof rootObject['toJSON'] === 'function') { visitorCallback('toJSON') } } else { for (var propertyName in rootObject) { visitorCallback(propertyName) diff --git a/packages/observable/src/subscribable.ts b/packages/observable/src/subscribable.ts index 5f5354df..34b2cbcc 100644 --- a/packages/observable/src/subscribable.ts +++ b/packages/observable/src/subscribable.ts @@ -136,7 +136,7 @@ var ko_subscribable_fn = { }) }, - when (test, returnValue) { + when (test: any, returnValue: unknown) { const current = this.peek() const givenRv = arguments.length > 1 const testFn = typeof test === 'function' ? test : v => v === test