Add flag to change catch variables' default types to unknown#41013
Add flag to change catch variables' default types to unknown#41013DanielRosenwasser merged 9 commits intomainfrom
catch variables' default types to unknown#41013Conversation
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 |
|
@DanielRosenwasser Here they are:Comparison Report - master..41013
System
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sandersn
left a comment
There was a problem hiding this comment.
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.
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.
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. |
sandersn
left a comment
There was a problem hiding this comment.
👍🏼 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 <bot@renovateapp.com> Co-authored-by: Amir Zarrinkafsh <nightah@me.com>
|
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
useUnknownInCatchVariableswhich 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 typeanyto revert to the old default behavior.Fixes #41016.