Skip to content

Commit 43fb9ff

Browse files
committed
fix: update imports and improve TypeScript typings across binding handlers
1 parent f829891 commit 43fb9ff

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

packages/bind/spec/bindingHandlerBehaviors.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { VirtualProvider } from '@tko/provider.virtual'
1010

1111
import { DataBindProvider } from '@tko/provider.databind'
1212

13-
import { applyBindings, BindingHandler, contextFor } from '../dist'
13+
import { applyBindings, BindingHandler, contextFor } from '../src'
1414

1515
import { bindings as coreBindings } from '@tko/binding.core'
1616
import { bindings as templateBindings } from '@tko/binding.template'
@@ -48,9 +48,9 @@ describe('BindingHandler behaviors', function () {
4848
v: Observable
4949
x: Observable
5050
y: Observable
51-
declare computed: any
52-
constructor(...args) {
53-
super(...args)
51+
52+
constructor(params) {
53+
super(params)
5454
const v = (this.v = koObservable(0))
5555
instance = this
5656
this.x = this.computed(() => {
@@ -92,9 +92,8 @@ describe('BindingHandler behaviors', function () {
9292
let obs = koObservable(),
9393
handlerInstance
9494
bindingHandlers.fnHandler = class extends BindingHandler {
95-
declare subscribe: any
96-
constructor(...args) {
97-
super(...args)
95+
constructor(params) {
96+
super(params)
9897
handlerInstance = this
9998
this.subscribe(obs, this.cb)
10099
}
@@ -110,7 +109,7 @@ describe('BindingHandler behaviors', function () {
110109
})
111110

112111
it('registers a kind with HandlerClass.register', function () {
113-
class HC extends BindingHandler {}
112+
class HC extends BindingHandler<any> {}
114113

115114
BindingHandler.registerBindingHandler(HC, 'testHCregistration')
116115
expect(bindingHandlers.testHCregistration).toEqual(HC)

packages/bind/src/BindingHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export class BindingHandler<T = any> extends LifeCycle {
9797
provider.bindingHandlers.set(name, this) //todo dangerous javascript: this in static function = this is calling object
9898
}
9999

100-
static registerBindingHandler(handler: BindingHandler, name: string, provider = options.bindingProviderInstance) {
100+
static registerBindingHandler(
101+
handler: typeof BindingHandler,
102+
name: string,
103+
provider = options.bindingProviderInstance
104+
) {
101105
provider.bindingHandlers.set(name, handler)
102106
}
103107
}

packages/binding.core/src/textInput.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ class TextInput extends BindingHandler {
1717
return 'textinput'
1818
}
1919

20-
declare $element: HTMLInputElement
20+
get $inputElement(): HTMLInputElement {
21+
return this.$element as HTMLInputElement
22+
}
23+
2124
previousElementValue: any
2225
elementValueBeforeEvent?: ReturnType<typeof setTimeout>
2326
timeoutHandle?: ReturnType<typeof setTimeout>
2427

2528
constructor(...args: [any]) {
2629
super(...args)
27-
this.previousElementValue = this.$element.value
30+
31+
this.previousElementValue = this.$inputElement.value
2832

2933
if (options.debug && (this.constructor as any)._forceUpdateOn) {
3034
// Provide a way for tests to specify exactly which events are bound
@@ -58,7 +62,7 @@ class TextInput extends BindingHandler {
5862
}
5963

6064
updateModel(event) {
61-
const element = this.$element
65+
const element = this.$inputElement
6266
clearTimeout(this.timeoutHandle)
6367
this.elementValueBeforeEvent = this.timeoutHandle = undefined
6468
const elementValue = element.value
@@ -73,7 +77,7 @@ class TextInput extends BindingHandler {
7377
}
7478

7579
deferUpdateModel(event: any) {
76-
const element = this.$element
80+
const element = this.$inputElement
7781
if (!this.timeoutHandle) {
7882
// The elementValueBeforeEvent variable is set *only* during the brief gap between an
7983
// event firing and the updateModel function running. This allows us to ignore model
@@ -92,12 +96,12 @@ class TextInput extends BindingHandler {
9296
}
9397
if (this.elementValueBeforeEvent !== undefined && modelValue === this.elementValueBeforeEvent) {
9498
setTimeout(this.updateView.bind(this), 4)
95-
} else if (this.$element.value !== modelValue) {
99+
} else if (this.$inputElement.value !== modelValue) {
96100
// Update the element only if the element and model are different. On some browsers, updating the value
97101
// will move the cursor to the end of the input, which would be bad while the user is typing.
98102
this.previousElementValue = modelValue // Make sure we ignore events (propertychange) that result from updating the value
99-
this.$element.value = modelValue
100-
this.previousElementValue = this.$element.value // In case the browser changes the value (see #2281)
103+
this.$inputElement.value = modelValue
104+
this.previousElementValue = this.$inputElement.value // In case the browser changes the value (see #2281)
101105
}
102106
}
103107
}

packages/binding.core/src/value.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class value extends BindingHandler {
1111
return ['options', 'foreach', 'template']
1212
}
1313

14-
declare $element: HTMLInputElement
1514
elementValueBeforeEvent: any
1615
propertyChangeFired: any
1716
propertyChangedFired: boolean
@@ -36,7 +35,7 @@ export class value extends BindingHandler {
3635

3736
arrayForEach(this.eventsToCatch, eventName => this.registerEvent(eventName))
3837

39-
if (this.isInput && this.$element.type === 'file') {
38+
if (this.isInput(this.$element) && this.$element.type === 'file') {
4039
this.updateFromModel = this.updateFromModelForFile
4140
} else {
4241
this.updateFromModel = this.updateFromModelForValue
@@ -52,20 +51,20 @@ export class value extends BindingHandler {
5251
return [...new Set(['change', ...requestedEventsArray])]
5352
}
5453

55-
get isInput() {
54+
isInput(element: HTMLElement): element is HTMLInputElement {
5655
return tagNameLower(this.$element) === 'input'
5756
}
5857

5958
get isCheckboxOrRadio() {
6059
const e = this.$element
61-
return this.isInput && (e.type == 'checkbox' || e.type == 'radio')
60+
return this.isInput(e) && (e.type == 'checkbox' || e.type == 'radio')
6261
}
6362

6463
// Workaround for https://github.com/SteveSanderson/knockout/issues/122
6564
// IE doesn't fire "change" events on textboxes if the user selects a value from its autocomplete list
6665
get ieAutoCompleteHackNeeded() {
6766
return (
68-
this.isInput
67+
this.isInput(this.$element)
6968
&& this.$element.type == 'text'
7069
&& this.$element.autocomplete != 'off'
7170
&& (!this.$element.form || this.$element.form.autocomplete != 'off')
@@ -105,7 +104,7 @@ export class value extends BindingHandler {
105104
// For file input elements, can only write the empty string
106105
const newValue = unwrap(this.value)
107106
if (newValue === null || newValue === undefined || newValue === '') {
108-
this.$element.value = ''
107+
;(this.$element as HTMLInputElement).value = ''
109108
} else {
110109
dependencyDetection.ignore(this.valueUpdateHandler, this) // reset the model to match the element
111110
}

packages/lifecycle/src/LifeCycle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { addDisposeCallback, createSymbolOrString } from '@tko/utils'
44

55
import { computed } from '@tko/computed'
6+
import { Observable } from 'packages/observable'
67

78
const SUBSCRIPTIONS = createSymbolOrString('LifeCycle Subscriptions List')
89
const ANCHOR_NODE = createSymbolOrString('LifeCycle Anchor Node')
@@ -24,7 +25,7 @@ export default class LifeCycle {
2425
}
2526
}
2627

27-
subscribe(observable, action, subscriptionType) {
28+
subscribe(observable: Observable, action, subscriptionType?: string) {
2829
if (typeof action === 'string') {
2930
action = this[action]
3031
}

0 commit comments

Comments
 (0)