Skip to content

using being too strict to throw on Function type #54951

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!==void0){if(typeofvalue!=="object")thrownewTypeError("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:

exportdefaultfunctionisObject(obj){vartype=typeofobj;returntype==='function'||(type==='object'&&!!obj);}

⏯ Playground Link

functioncreateHandle(): Writer&Disposable{constwrite: Writer=(data)=>{};constclose=(): void=>{};returnObject.assign(write,{[Symbol.dispose]: close});}{
using write=createHandle();}typeWriter={(data: string): void;};

Playground link with relevant code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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