-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript) fix errors #202
base: main
Are you sure you want to change the base?
Conversation
add some types
#we should remove jasmine..
switch d.ts-version for jasmine (the project used jasmine 1.3)
1) [] => new Array 2) childNodes => children 3) extend some global type-defs
In the last few days we have done some more tests and can now officially submit our PR for approval. We hope to get closer to a release of TKO 4.0. I think we are ready for a production release! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good so far, I'm not done reviewing about 1/10th of the way, but wanted to add some interim comments and I'll continue to come back to this as time permits.
Thank you!
|
||
Note that running `karma` or `rollup` will create a `visual.html` file that shows the proportional size of imports into each package. | ||
Note that running `karma` will create a `visual.html` file that shows the proportional size of imports into each package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's an equivalent with esbuild [1] if we wanted to re-enable this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's cool, I didn't know that before
/// <reference types="jasmine" /> | ||
/// <reference types="jquery" /> | ||
|
||
export { }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the export for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nice to add a comment to document its purpose)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export {}
allows redefining/type-merging the window module in d.ts files, see https://bobbyhadz.com/blog/typescript-make-types-global. I have add a comment.
|
||
export { }; | ||
|
||
declare global { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally want to be mindful of polluting the global namespace, so we will need to be careful to not distribute this, and not rely on types defined only here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree to 100%. The best global.d.ts is an empty one. Some oldstyle global objects like jquery or required need to be currently declared. The Jasmine and Mocha problem is homemade, but removing Jasmine is a bigger task.
For compatibility, some global knockout types remain here because refactoring leads to invasive changes. Changing the prototype chains of the TKO base classes to js/ts classes can be done in later steps, but it breaks some of the critical interfaces of knockoutjs. We had try that.
I will reorganise some global declarations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: "Import type" was a game changer. I also found a solution to create an object using a function in TypeScript like JavaScript for 'subscribable'. Changing the prototype chain of the objects (classes) in @tko/observable is a different challenge.
o: () => ObservableArray // For jsxBehaviors Test | ||
} | ||
|
||
//Jasmine and Mocha define duplicated functions, is a problem for the type system |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how it works for now. Jasmine 1.3 is old and there aren't many tests in Mocha. Perhaps a facade that redirects Jasmine 1.3 to Mocha/Chai is a future solution.
@@ -244,14 +244,15 @@ describe('Deferred bindings', function () { | |||
|
|||
// Value is initially true, so nodes are retained | |||
applyBindings({ someItem: someItem, counter: 0 }, testNode) | |||
expect(testNode.childNodes[0].childNodes[0].tagName.toLowerCase()).toEqual('span') | |||
|
|||
expect(testNode.children[0].children[0].tagName.toLowerCase()).toEqual('span') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unclear why this expectation has changed / needs changing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
children
is instance of HTMLCollection
so tagName is defined on each Element
. I will change the other usings of childNodes in this test-case, too.
@@ -449,7 +450,7 @@ describe('Binding dependencies', function () { | |||
expect(vm.getSubscriptionsCount()).toEqual(1); | |||
|
|||
// remove the element and re-set the view-model; the subscription should be cleared | |||
testNode.parentNode.removeChild(testNode); | |||
testNode.parentNode?.removeChild(testNode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems odd that ?.
is needed, suggests the test is unstable/flaky (but it may be ok - just noting)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i added expect(testNode.parentElement).not.toBeNull();
(parentNode
is optional in HTMLElement)
@@ -53,6 +53,11 @@ describe('BindingHandler behaviors', function () { | |||
var xCalls = 0, | |||
yCalls = 0 | |||
bindingHandlers.fnHandler = class extends BindingHandler { | |||
|
|||
v : any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just looking at just this file, it's unclear what v, x, y, computed are doing here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tests are very interesting.. The member variables are now declared.
@@ -66,11 +65,11 @@ describe('Cross-window support', function () { | |||
|
|||
win2.document.write(BLANK_HTML) | |||
win2.document.close() | |||
body2 = win2.document.body | |||
const body2 = win2.document.body |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With eslint we can later auto fix all definitions to let or const.. we are all already prepared, see https://github.com/Auge19/tko/tree/add_eslint
|
||
// renderTemplate | ||
window.runs(function () { | ||
setTemplateEngine(new dummyTemplateEngine({ someTemplate: "<div data-bind='text: text'></div>" }), true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unclear why we're removing , true
(may see it later, just noting)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -2,6 +2,7 @@ | |||
|
|||
import { applyBindingsToDescendants } from './applyBindings' | |||
import { AsyncBindingHandler } from './BindingHandler' | |||
import { BindingResult } from "./BindingResult"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be import type { BindingResult } ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, it could also be a solution for the remaining global KO-defs, so we can move them into the associated modules in a second step. @mcselle FYI "Type-Only Imports": https://www.typescriptlang.org/docs/handbook/modules/reference.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
As announced, we have started to make TKO more type-safe. To get a startpoint, we have set the tsconfig as softly as possible and continue to allow "any" types.
We (@Auge19 , @mcselle ) validate the typescript code with tsc. The current status is only 313 errors left. We began with 7410 errors. With this draft PR we would like to get feedback and start our own review. The goal is 0 errors with tsc. If this PR is ready and merged, we would like to continue with ESLINT.
All tests continue to pass.