microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.04k stars 12.49k forks source link

useUnknownInCatchVariables would do much better to require ": unknown" rather than magically injecting it #59100

Open bkerin opened 4 months ago

bkerin commented 4 months ago

🔎 Search Terms

useUnknownInCatchVariables

🕗 Version & Regression Information

⏯ Playground Link

No response

💻 Code

No response

🙁 Actual behavior

With useUnknownInCatchVariables :unknonw is magically injected after err (in catch (err))

try {
  console.log("in try")
}
catch (err) {
  console.log("in catch")
  console.log(err.message)
}

This results in a mysterious failure like so (from deno i.e. swc):

error: TS18046 [ERROR]: 'err' is of type 'unknown'.
    console.log(err.message)

The design of this option is very wrong, it's not worth the tiny saving of not having to type :unknown give how mysterious the above error is for a user who isn't aware of this option. It should have been designed to require this declaration rather than injecting it.

I realize this option can't be fixed at this point and it's probably not worth introducing a similar one with the better behavior. Perhaps as an alternative swc (and maybe other compilers) could be fixed to give an error that at least mentions useUnknownInCatchVariables.

🙂 Expected behavior

Something less mysterious :)

Additional information about the issue

None

DanielRosenwasser commented 4 months ago

I don't think we're changing how the option works at this point, but we could possibly give a better error message here.