[node]: Allow timer functions to work with callbacks with a single unknown argument#54286
Conversation
|
@birtles Thank you for submitting this PR! This is a live comment which I will keep updated. 1 package in this PRCode ReviewsBecause this is a widely-used package, a DT maintainer will need to review it before it can be merged. You can test the changes of this PR in the Playground. Status
All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes. Diagnostic Information: What the bot saw about this PR{
"type": "info",
"now": "-",
"pr_number": 54286,
"author": "birtles",
"headCommitOid": "376e4e828308661aa8512cff74cd926550b6c457",
"lastPushDate": "2021-07-07T01:14:28.000Z",
"lastActivityDate": "2021-07-08T23:42:03.000Z",
"mergeOfferDate": "2021-07-08T17:38:29.000Z",
"mergeRequestDate": "2021-07-08T23:42:03.000Z",
"mergeRequestUser": "birtles",
"hasMergeConflict": false,
"isFirstContribution": false,
"tooManyFiles": false,
"popularityLevel": "Critical",
"pkgInfo": [
{
"name": "node",
"kind": "edit",
"files": [
{
"path": "types/node/test/timers.ts",
"kind": "test"
},
{
"path": "types/node/timers.d.ts",
"kind": "definition"
}
],
"owners": [
"Microsoft",
"DefinitelyTyped",
"jkomyno",
"alvis",
"r3nya",
"btoueg",
"smac89",
"touffy",
"DeividasBakanas",
"eyqs",
"Hannes-Magnusson-CK",
"KSXGitHub",
"hoo29",
"kjin",
"ajafff",
"islishude",
"mwiktorczyk",
"mohsen1",
"n-e",
"galkin",
"parambirs",
"eps1lon",
"SimonSchick",
"ThomasdenH",
"WilcoBakker",
"wwwy3y3",
"samuela",
"kuehlein",
"bhongy",
"chyzwar",
"trivikr",
"nguymin4",
"yoursunny",
"qwelias",
"ExE-Boss",
"Ryan-Willpower",
"peterblazejewicz",
"addaleax",
"JasonHK",
"victorperin",
"ZYSzys"
],
"addedOwners": [],
"deletedOwners": [],
"popularityLevel": "Critical"
}
],
"reviews": [
{
"type": "approved",
"reviewer": "rbuckton",
"date": "2021-07-08T17:37:55.000Z",
"isMaintainer": true
},
{
"type": "stale",
"reviewer": "SimonSchick",
"date": "2021-07-06T17:32:56.000Z",
"abbrOid": "6be917a"
}
],
"mainBotCommentID": 873850293,
"ciResult": "pass"
} |
|
🔔 @microsoft @DefinitelyTyped @jkomyno @alvis @r3nya @btoueg @smac89 @Touffy @DeividasBakanas @eyqs @Hannes-Magnusson-CK @KSXGitHub @hoo29 @kjin @ajafff @islishude @mwiktorczyk @mohsen1 @n-e @galkin @parambirs @eps1lon @SimonSchick @ThomasdenH @WilcoBakker @wwwy3y3 @samuela @kuehlein @bhongy @chyzwar @trivikr @nguymin4 @yoursunny @qwelias @ExE-Boss @Ryan-Willpower @peterblazejewicz @addaleax @JasonHK @victorperin @ZYSzys — please review this PR in the next few days. Be sure to explicitly select |
|
I feel like TBH I think that error produced by is correct and in this exact case user should use new promise variants from node v16, however I understand that this might be a problem of wider compatibility browser/node or v14/v16. Though I'm still convinced that promise is a problem, not a timers. Edit:
declare interface PromiseConstructor {
new <T = void>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
}
|
Yes, I agree. Like you my concern is we don't break code like If we can get the |
Maintainers have probably some proven way to deal with such transitions. Maybe even including changed
Don't think so, |
|
Some references: There was also issue microsoft/TypeScript#41497 but I think it was one big misunderstanding and nobody was rising |
SimonSchick
left a comment
There was a problem hiding this comment.
This makes sense to me, while it's unfortunate that we have to work around this this way, the solution looks OK (minus maybe my suggested change).
Changing the Promise type in core TS definitions is probably not going to happen and very out of scope of this issue.
| } | ||
|
|
||
| function setTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout; | ||
| function setTimeout(callback: (args: unknown) => void): NodeJS.Timeout; |
There was a problem hiding this comment.
It might actually be better to use void rather than unknown.
Also please add a brief comment on why these rather weird overloads exist for future ref.
There was a problem hiding this comment.
I've updated the PR now with this feedback. I hope the comments make sense.
|
I have to agree with Simon. I was messing a bit with And change |
|
@birtles The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
Apparently we can't use Explanation here: https://github.com/Microsoft/dtslint/blob/master/docs/void-return.md Should this really be: ? |
|
Maybe |
Thanks! Updated as suggested. |
|
@SimonSchick Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
| function clearTimeout(timeoutId: NodeJS.Timeout): void; | ||
|
|
||
| function setInterval<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timer; | ||
| // As with setTimeout, overload to allow setInterval to take a Promise |
There was a problem hiding this comment.
This is excessive, can be reduced to // util.promisify no rest args compability
…known argument Fixes DefinitelyTyped#54285.
|
@SimonSchick Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
|
Ping @rbuckton |
rbuckton
left a comment
There was a problem hiding this comment.
This looks fine. Supporting setInterval seems iffy, but doesn't hurt anything.
| function setInterval<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timer; | ||
| // util.promisify no rest args compability | ||
| // tslint:disable-next-line void-return | ||
| function setInterval(callback: (args: void) => void, ms?: number): NodeJS.Timer; |
There was a problem hiding this comment.
Not sure how useful this is for setInterval since you can only resolve a promise once.
There was a problem hiding this comment.
Promisified setInterval returns an async iterator.
There was a problem hiding this comment.
As far as wrapping it in a new Promise(...) goes I'd with setInterval, seems extremely niche.
There was a problem hiding this comment.
I'm not sure but there may be other circumstances apart from Promises where this comes up. This comment may be from a non-Promise situation judging by the function name.
I think it may be best to just merge this now to keep it consistent.
|
Ready to merge |
|
I just published |
| function setTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout; | ||
| // util.promisify no rest args compability | ||
| // tslint:disable-next-line void-return | ||
| function setTimeout(callback: (args: void) => void): NodeJS.Timeout; |
There was a problem hiding this comment.
Sorry, I know this PR has already been merged, but shouldn't this have the ms parameters as for setInterval?
I still have the Expected 3 arguments, but got 2 error for setTimeout but not for setInterval
There was a problem hiding this comment.
You're right. Good catch. CC @birtles
@SimonSchick Seems like tests are passing because of "dom" lib in tsconfig.json. How to test this properly?
There was a problem hiding this comment.
It's kinda tricky, in CI we need to test it with the dom lib to ensure compatibility, you can always temporarily remove it test, add it back, I understand this is a tiny bit of churn but should be OK for now.
Fixes #54285.
Please fill in this template.
npm test <package to test>.Select one of these and delete the others:
If changing an existing definition: