Skip to content

using being too strict to throw on Function type #54951

@rixtox

Description

@rixtox

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions