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

using downleveling fails when returning a disposable function #3390

Closed
laverdet opened this issue Sep 16, 2023 · 1 comment
Closed

using downleveling fails when returning a disposable function #3390

laverdet opened this issue Sep 16, 2023 · 1 comment

Comments

@laverdet
Copy link

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

@evanw
Copy link
Owner

evanw commented Sep 16, 2023

Thanks for the report. Looks like TypeScript recently changed their implementation of this feature: microsoft/TypeScript@02e8d5e. I can update esbuild to match the new behavior.

cpiro added a commit to cpiro/esbuild that referenced this issue Sep 16, 2023
@evanw evanw closed this as completed in e7bf735 Sep 16, 2023
GregBrimble pushed a commit to GregBrimble/esbuild that referenced this issue Apr 4, 2024
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

No branches or pull requests

2 participants