-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Add flag to change catch
variables' default types to unknown
#41013
Conversation
87b36c9
to
bbb3bfb
Compare
bbb3bfb
to
cccf22a
Compare
The TypeScript team hasn't accepted the linked issue #41016. If you can get it accepted, this PR will have a better chance of being reviewed. |
It would be awesome if |
7c01840
to
f529457
Compare
@typescript-bot pack this |
Heya @DanielRosenwasser, I've started to run the extended test suite on this PR at a32013b. You can monitor the build here. |
Heya @DanielRosenwasser, I've started to run the parallelized Definitely Typed test suite on this PR at a32013b. You can monitor the build here. |
Heya @DanielRosenwasser, I've started to run the parallelized community code test suite on this PR at a32013b. You can monitor the build here. |
Heya @DanielRosenwasser, I've started to run the perf test suite on this PR at a32013b. You can monitor the build here. Update: The results are in! |
Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at a32013b. You can monitor the build here. |
Hey @DanielRosenwasser, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commandLineParser entry should make the new flag a strict one, I think.
@@ -0,0 +1,35 @@ | |||
// @useUnknownInCatchVariables: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except for this, I'm not convinced we even need a new test, since we know that unknown
narrows, and from some other tests, that strict changes the type of the catch variable to unknown.
I guess there's nothing to change here, except possibly dropping the narrowing tests from this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added that style of code in because most of the purpose of this feature is to ensure that catch variables are narrowed, and that we error on usages if a user doesn't do so.
Meta: I was confused by the initial comment, because I wasn't familiar with |
@gvanrossum good call, thanks! Must have been an earlier copy/paste error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼 Does what we talked about in the design meeting.
Side note: It's a little worrying that we don't already have more tests with both (1) catch
and (2) strict: true
.
Fixes the `no-throw-literal` rule configuration for TypeScript by enabling the appropriate rule in the TypeScript config, per [documentation](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-throw-literal.md#how-to-use). This is timely due to recent [changes to TypeScript](microsoft/TypeScript#41013).
Typescript 4.x changes the default behaviour of try catch and its err type from `any` to [`unknown`](microsoft/TypeScript#41013). This change ensures that where we rely on said variable it is cast accordingly as an `Error`.
* build(deps): update dependency typescript to v4.4.2 * fix(web): cast try catch err type to error Typescript 4.x changes the default behaviour of try catch and its err type from `any` to [`unknown`](microsoft/TypeScript#41013). This change ensures that where we rely on said variable it is cast accordingly as an `Error`. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: Amir Zarrinkafsh <[email protected]>
Is there a way to type a function that it throws specific types? So that catch could know what to expect. Like a func that can throw |
Should the flag |
@guilhermesimoes #45602 proposes that idea. |
This change adds a new flag called
useUnknownInCatchVariables
which changes the default type of catch clause variables fromany
(today's existing behavior) tounknown
.More specifically, under this flag, the following code
would become equivalent to
As a result, a user would receive the following error message from TypeScript:
To mitigate this, a user could explicitly perform runtime checks
or if that is too painful, a user could use a type assertion to
any
, or provide an explicit annotation on the catch clause variable with the typeany
to revert to the old default behavior.Fixes #41016.