-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WebUI: Convert JS files to TypeScript #23245
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
base: master
Are you sure you want to change the base?
Conversation
ad2e176
to
662bfe1
Compare
ce4d954
to
997bedd
Compare
js-beautify doesn't support TS and mangles some of the type definitions.
997bedd
to
ebd1cc5
Compare
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 a chunky one. Not sure if it's a good idea to introduce so many changes here (including functional ones), as it could be too difficult to comprehend for reviewers.
type ElementTypeMap = { | ||
input: HTMLInputElement; | ||
select: HTMLSelectElement; | ||
image: HTMLImageElement; | ||
button: HTMLButtonElement; | ||
ul: HTMLUListElement; | ||
li: HTMLLIElement; | ||
frame: HTMLIFrameElement; | ||
template: HTMLTemplateElement; | ||
}; |
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 is default HTMLElementTagNameMap
type.
const getElementById = <T extends keyof ElementTypeMap>(id: string, type: T): ElementTypeMap[T] => { | ||
return document.getElementById(id) as ElementTypeMap[T]; | ||
}; |
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.
This is not safe, document.getElementById
returns null
if element doesn't exist.
Do we have a contract that all used elements would always exist?
Generally speaking, it's better to avoid as
casts when possible. This case would require a runtime check though.
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.
This is meant to be a direct replacement for existing code that calls document.getElementById()
. The existing code always assumes the requested element exists, so this new code keeps that behavior. In the future I think it would be useful to refactor that.
This was intended to be as few changes as necessary to get TS compilation working with any amount of type safety. The only large-ish refactor was introducing |
But My proposal was to (at least initially) keep all type checking stuff purely on the dev's side. |
I don't think there's any point in using TS if we allow type errors. CI should enforce that there aren't any type errors in order to merge. I can break up the current PR if it's easier to review, but I do think these changes are ultimately necessary. |
I'm talking about primary build script. End users / packagers should not hit that IMO. |
I'm not completely following. How would the primary build script differ? And how would we enforce "no new type issues" in CI if we already have ~700 type issues? |
As I said, we simply don't enforce it for now. Check locally, submit. But if you insist on enforcing it, I'm not in a position to argue 🙂 |
@Chocobo1 do you have any thoughts on this? |
This PR convert all JS in
src/webui/www/private/scripts
to TS. The typechecking isn't super strict yet (it allows implicitany
), but this gives us a good starting point. From here, we can then export strict types from all of our modules (i.e.window.qBittorrent.*
classes).To serve as an example of strict module typing, a few modules have been modified to properly export their types. This includes
AddTorrent
,Cache
,ColorScheme
,ContextMenu
,FileTree
andMisc
. All usage ofwindow.qBittorrent.$MODULE
is type checked.