-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flag to type 'catch' variables as 'unknown'. #41016
Comments
@DanielRosenwasser You might want to proofread the title. |
GitHub seems to be having UI glitches for me or something.... |
Default in tsc --init please? :) |
@DanielRosenwasser will this flag be included in |
I believe this should be the default behavior, actually. The inferred type for
|
@gerardolima TypeScript usually does not introduce breaking changes without a very good reason, which I'd argue this is not. It's in the same realm with the strict compiler flags, which all are breaking changes and are opt-in. |
Only code that rely on wrong assumptions (and |
So through a personal Twitter survey, current results show that many people have voted for That said, there are two things worth mentioning:
|
I suspect you could broadly split TypeScript devs into two camps, those who're migrating from JavaScript or otherwise just want a barely-typed experience, and those who want their type system to be as safe as possible thus want to avoid Totally anecdotal, but in codebases I've worked on where we've made use of try/catch for control flow we seldom ever treated it as anything other than |
i suspect the camp migrating from javascript is on the decline - at least in my experience nobody starts a project in plain JS any more when it's possible to use TS instead. and - having just migrated a medium-sized project from JS to TS - i'd still be glad for any help the typechecker can give me to make things right while i'm at it. even if it takes a bit more effort. |
@ritschwumm There are many projects out there that don't support strict type checks, and new ones are still created each day. Two examples I recently had to suffer from are Angular (which just recently started supporting So I wouldn't say the only issue are projects migrating from JS. |
@samhh the following code fails with @typescript-eslint rules due to no-unsafe-assignment; that's what I want to avoid. Variables typed as try { throw new Error('any-error') } catch(err) { console.log({ err }) } |
Just want to mention there's a typescript-eslint rule to disallow inferring of the catch type (to enforce using Would be great to have it integrated in typescript though. |
In TypeScript 4.0, we allowed users to to annotate
catch
variables with: unknown
; however, it'd be nice if we could have that be the default.I could imagine a flag like
--useUnknownInCatchVariables
to switch the default type to beunknown
.Alternatively, I could also imagine a much broader flag that also types parameters as
unknown
as well.The text was updated successfully, but these errors were encountered: