Skip to content

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

Description

@fabian-paus

``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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions