Skip to content
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

Open
wants to merge 229 commits into
base: main
Choose a base branch
from
Open

Typescript) fix errors #202

wants to merge 229 commits into from

Conversation

phillipc
Copy link
Contributor

@phillipc phillipc commented Jan 12, 2025

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.

phillipc and others added 30 commits December 20, 2024 21:45
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
fix wrong jasmine imports
fix method signature
@phillipc phillipc marked this pull request as ready for review February 14, 2025 13:54
@phillipc
Copy link
Contributor Author

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!

Copy link
Member

@brianmhunt brianmhunt left a 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.
Copy link
Member

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

[1] https://esbuild.github.io/analyze/

Copy link
Contributor Author

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 { };
Copy link
Member

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?

Copy link
Member

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)

Copy link
Contributor Author

@phillipc phillipc Feb 22, 2025

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 {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@phillipc phillipc Feb 23, 2025

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

Copy link
Contributor Author

@phillipc phillipc Feb 24, 2025

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')
Copy link
Member

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

Copy link
Contributor Author

@phillipc phillipc Feb 22, 2025

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);
Copy link
Member

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)

Copy link
Contributor Author

@phillipc phillipc Feb 22, 2025

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
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

@phillipc phillipc Feb 22, 2025

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)
Copy link
Member

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)

Copy link
Contributor Author

@phillipc phillipc Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummyTemplateEngine and setTemplateEngine have only one parameter, now we a type-safe(r) 👯‍♀️ 👯‍♀️
grafik

@@ -2,6 +2,7 @@

import { applyBindingsToDescendants } from './applyBindings'
import { AsyncBindingHandler } from './BindingHandler'
import { BindingResult } from "./BindingResult";
Copy link
Member

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 } ...?

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants