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

Compatibility with Node 20: NodeJS.Timer vs NodeJS.Timeout #341

Closed
fabian-paus opened this issue Sep 5, 2023 · 1 comment · Fixed by #342
Closed

Compatibility with Node 20: NodeJS.Timer vs NodeJS.Timeout #341

fabian-paus opened this issue Sep 5, 2023 · 1 comment · Fixed by #342

Comments

@fabian-paus
Copy link

``There seems to be a compatibility issue when using emailjs with Node 20 and TypeScript, since the Timeout interface has a new dispose method:

../node_modules/emailjs/smtp/client.ts:165:17 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(timeoutId: string | number | Timeout | undefined): void', gave the following error.
    Argument of type 'Timer' is not assignable to parameter of type 'string | number | Timeout | undefined'.
      Property '[Symbol.dispose]' is missing in type 'Timer' but required in type 'Timeout'.
  Overload 2 of 2, '(id: number | undefined): void', gave the following error.
    Argument of type 'Timer' is not assignable to parameter of type 'number'.

165    clearTimeout(this.timer);
                    ~~~~~~~~~~

  ../node_modules/@types/node/timers.d.ts:126:17
    126                 [Symbol.dispose](): void;
                        ~~~~~~~~~~~~~~~~
    '[Symbol.dispose]' is declared here.


Found 1 error in ../node_modules/emailjs/smtp/client.ts:165

The issue seems to be cause by the type annotation in client.ts

protected timer: NodeJS.Timer | null = null;

The type NodeJS.Timer is a legacy type that does not have the dispose method.
Later the setTimeout method actually returns a NodeJS.Timeout (with the required dispose method).
The error is caused by the clearTimeout call, that requires a NodeJS.Timeout but is given a NodeJS.Timer:

if (this.timer != null) {
    clearTimeout(this.timer);
}

Potential fix: Annotate timer with the correct type

protected timer: NodeJS.Timeout | null = null;

Or potentially be more generic:

protected timer: ReturnType<typeof setTimeout> | null = null;

Versions:

  • Node: v20.5.1
  • @types/node: 20.5.9
  • emailjs: 4.0.2
@eleith
Copy link
Owner

eleith commented Sep 6, 2023

awesome. thanks for the breakdown. will resolve with one of your suggestions.

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 a pull request may close this issue.

2 participants