Skip to content

using downleveling fails when returning a disposable function #3390

@laverdet

Description

@laverdet

Given the source file:

// @ts-expect-error
Symbol.dispose = Symbol();
const fn: any = () => {};
fn[Symbol.dispose] = () => console.log("dispose");
using handle = fn;

If we run through esbuild:

-> % npx esbuild --target=node18 test.ts | node
[stdin]:40
      throw error;
      ^

TypeError: Object expected
    at __using ([stdin]:10:13)
    at [stdin]:50:16
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:313:38)
    at node:internal/process/execution:79:19
    at [stdin]-wrapper:6:22
    at evalScript (node:internal/process/execution:78:60)
    at node:internal/main/eval_stdin:30:5
    at Socket.<anonymous> (node:internal/process/execution:195:5)
    at Socket.emit (node:events:525:35)

Node.js v18.12.1

esbuild requires that the disposable resource is an object (not function):

// [...]
    if (typeof value !== "object")
      throw TypeError("Object expected");

From TypeScript it executes cleanly:

marcel@marcel [11:04:52] [~/tmp] 
-> % npx tsc && node dist/test.js
dispose

The emitted file from tsc explicitly handles the function case:

// [...]
        if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
        var dispose;

Relevant specification doesn't say that the disposable may not be a function:
https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod

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