microsoft / microsoft/TypeScript

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

Open
#59,100 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Error Messages Experience Enhancement Needs Proposal
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

useUnknownInCatchVariables

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about useUnknownInCatchVariables
⏯ 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the useUnknownInCatchVariables option and the TS18046 diagnostic. Begin by reviewing how that option produces the diagnostic in catch blocks. Done means the resulting behavior or error explains the option clearly, with the expected approach agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.