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 being too strict to throw on Function type #54951

Closed
rixtox opened this issue Jul 10, 2023 · 1 comment · Fixed by #54969
Closed

using being too strict to throw on Function type #54951

rixtox opened this issue Jul 10, 2023 · 1 comment · Fixed by #54969
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@rixtox
Copy link

rixtox commented Jul 10, 2023

Bug Report

Currently the polyfilled implementation for using performs the following check:

var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
    if (value !== null && value !== void 0) {
        if (typeof value !== "object") throw new TypeError("Object expected.");
// ...

In the spec, the value used with using needs to be an "Object type", but we know in JavaScript an Object type can have typeof value other than "object", for example, a function has typeof value === "function" while also being a valid JavaScript Object.

The implementation should use the check similar to underscore.js's isObject() check:

export default function isObject(obj) {
  var type = typeof obj;
  return type === 'function' || (type === 'object' && !!obj);
}

⏯ Playground Link

function createHandle(): Writer & Disposable {
    const write: Writer = (data) => {};
    const close = (): void => {};
    return Object.assign(write, { [Symbol.dispose]: close });
}

{
    using write = createHandle();
}

type Writer = {
    (data: string): void;
};

Playground link with relevant code

@rixtox
Copy link
Author

rixtox commented Jul 10, 2023

CC @rbuckton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants