fix(54951): using being too strict to throw on Function type (#54969)

This commit is contained in:
Oleksandr T
2023-07-18 01:32:25 +03:00
committed by GitHub
parent 94f03cf0c6
commit 02e8d5ec61
54 changed files with 78 additions and 53 deletions

View File

@@ -1403,7 +1403,7 @@ export const addDisposableResourceHelper: UnscopedEmitHelper = {
text: `
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
if (value !== null && value !== void 0) {
if (typeof value !== "object") throw new TypeError("Object expected.");
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
var dispose;
if (async) {
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");

View File

@@ -1658,4 +1658,29 @@ describe("unittests:: evaluation:: usingDeclarations", () => {
"after block"
]);
});
it("'using' for 'function' disposable resource ", () => {
const { main, output } = evaluator.evaluateTypeScript(`
export const output: any[] = [];
function disposable() {
const f = () => output.push("enter");
const d = () => output.push("exit");
return Object.assign(f, { [Symbol.dispose]: d });
}
export function main() {
using run = disposable();
run();
}
`, { target: ts.ScriptTarget.ES2018 });
main();
assert.deepEqual(output, [
"enter",
"exit",
]);
});
});