Skip to content

In typing, setTimeout doesn't accept string type as callback #30166

@kt3k

Description

@kt3k

In lib.deno.shared_globals.d.ts, setTimeout is typed as below, and doesn't accept string as callback.

declare function setTimeout(
  /** callback function to execute when timer expires */
  cb: (...args: any[]) => void,
  /** delay in ms */
  delay?: number,
  /** arguments passed to callback function */
  ...args: any[]
): number;

On the other hand, in lib.dom.d.ts, setTimeout is typed as:

setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;

where TimerHandler is string | Function.

In Deno's setTimeout implementation, string callback seems explicitly supported:

// If callback is a string, replace it with a function that evals the string on every timeout
if (typeof callback !== "function") {
const unboundCallback = webidl.converters.DOMString(callback);
callback = () => indirectEval(unboundCallback);
}

Is this discrepancy of setTimeout typing between lib.deno and lib.dom intentional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working correctlygood first issueGood for newcomerstypesIssues related to TypeScript types

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions