Draft: added typescript types to waitfor functions #982
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Converted 5 files from
.js
to.ts
. All files relate thewaitFor
functions.Why:
This PR continues the conversion of the codebase to TypeScript - issue #494
How:
(error as Error).name
and(node as unknown) as Screen).debug
.Element
orHTMLElement
toNode
in a few places in order to match the implementation, which shows that the container could be aDocument
as well.node
argument in thegetWindowFromNode
function as an object that maybe has properties of aWindow
,Document
, orElement
. This allows access to these properties withoutas
assertion.timerApi
object to avoid TS errors when adding additional properties after the object has been created.Checklist:
docs site
Issues
I'm considering this a draft because I still have lingering questions where I'm not sure of the desired behavior.
My biggest confusion is the
setTimeout
/clearTimeout
functions. The type for a timeout is anumber
in the DOM and aNodeJS.Timeout
object in node. Which one is it at runtime? Can it be either, or can it be typed cleanly as just one? The inferred type from using the globalclearTimeout
variable is that it's either a function which takes anumber
or a function that takes aNodeJS.Timeout
object. This creates an impossible function which cannot be called, as we don't know which of the two arguments it takes. I've had to circumvent it with a nasty type assertion that allows the function to be called with either type.I'm happy to explain the logic behind any of these changes.
There's a lot more assertions then I would normally make if I was writing this code from scratch. I generally prefer type guard functions, which refine the type of the variable when
true
. For example theisPromise
function which I put in thewait-for.ts
file would be better placed in utilities file where it can be exported and used in multiple places. Then this mess inhelpers.ts
can be replaced with
Is that acceptable and/or desirable? To me it seems like a no-brainer but I am trying to limit the scope of my changes as much as possible.