microsoft/TypeScript

Should reject using / await using in a switch case / default clause

Opened this issue ยท 1 comments

๐Ÿ”Ž Search Terms

explicit resource management, using, await using, switch, case clause, default clause

๐Ÿ•— Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

โฏ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=6.0.0-dev.20251030#code/M4dwlgLgxgFgBACgLIEMIwHQCcUDsAmA9gLYICUZcA3gFBxxQrACmcADAFx31wCuwYXAHM4+AExs4AXmpwA2gGUAnsQBGhADYZ8YYAAdCLALrlqAXzhmA3N3qqszFAGsb3RizgBGLjzgoQKJB8AsKiYp7Ssooq6lo6+obMJpRUFta2cPaOLjRmNDTMAB4GWBDmNEA

๐Ÿ’ป Code

switch (Math.random()) {
  case 0:
    using d20 = { [Symbol.dispose]() {} };
    break;

  case 1:
    await using d21 = { [Symbol.dispose]() {} };
    break;
}

export {}

๐Ÿ™ Actual behavior

No error happens.

๐Ÿ™‚ Expected behavior

The error is reported that using / await using cannot be used here.

Additional information about the issue

The spec was updated to disallow this (rbuckton/ecma262#14).
Related PRs in other projects:

Some tests needs to be updated. For example:

switch (Math.random()) {
case 0:
using d20 = { [Symbol.dispose]() {} };
break;
case 1:
using d21 = { [Symbol.dispose]() {} };
break;
}
if (true)
switch (0) {
case 0:
using d22 = { [Symbol.dispose]() {} };
break;
}

๐Ÿค– This is an automated response. I looked for things that might help (duplicates, FAQ entries, etc) but didn't find anything. A human will take a look!