Skip to content

fix(47383): Destructuring of unknown catch variable is not an error - #47442

Closed
Oleksandr Tarasiuk (a-tarasyuk) wants to merge 0 commit into
microsoft:mainfrom
a-tarasyuk:fix/47383
Closed

fix(47383): Destructuring of unknown catch variable is not an error#47442
Oleksandr Tarasiuk (a-tarasyuk) wants to merge 0 commit into
microsoft:mainfrom
a-tarasyuk:fix/47383

Conversation

@a-tarasyuk

Copy link
Copy Markdown
Contributor

Fixes#47383

@jakebailey

Copy link
Copy Markdown
Member

This PR does pass tests, but I'm a bit confused as to why it doesn't work when I run the build via tsserver locally in VS Code; do these sorts of checks need to be somewhere else, maybe? Some missing case in another function?

@jakebailey

Copy link
Copy Markdown
Member

Hm, apparently checkTryStatement doesn't actually call a check function on the catch clause / its variable declaration. That doesn't seem right.

@jakebailey

Copy link
Copy Markdown
Member

Yeah, I think checkTryStatement needs checkVariableLikeDeclaration(declaration), but that seems to cause some baseline changes including follow-on errors about implicit any, which are probably not right.

@jakebaileyJake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking as needing changes (meant to do this with my last comment), as this doesn't seem to work in VS Code (and probably other editors), so we'll need to make sure that check call is added. Not entirely certain how to ensure the tests actually walk that; maybe they already check the opposite?

@a-tarasyuk

Copy link
Copy Markdown
ContributorAuthor

Jake Bailey (@jakebailey) Thanks for the review and sorry for the delay in response. I've added changes that cover the following cases

// @strict: true// @useUnknownInCatchVariables: truetry{// ...}catch({ name }){^^^^Property'name'doesnotexistontype'unknown'// { name } has 'unknown' type because @useUnknownInCatchVariables: truename;}try{// ...}catch({ name } : unknown){^^^^Property'name'doesnotexistontype'unknown'// { name } has explicit type 'unknown'name;}
// @strict: true// @useUnknownInCatchVariables: falsetry{// ...}catch({ name }){// Ok// { name } has 'any' type because @useUnknownInCatchVariables: falsename;}try{// ...}catch({ name } : unknown){^^^^Property'name'doesnotexistontype'unknown'// { name } has explicit type 'unknown'name;}
// @strict: false// @useUnknownInCatchVariables: truetry{// ...}catch({ name }){^^^^Property'name'doesnotexistontype'{}'// { name } has '{}' type because @useUnknownInCatchVariables: true// `unknown` is represented as `{}` with disabled `strict: false` checkname;}try{// ...}catch({ name } : unknown){^^^^Property'name'doesnotexistontype`{}`// { name } has explicit type 'unknown'name;}
// @strict: false// @useUnknownInCatchVariables: truetry{// ...}catch({ name }){// Ok// { name } has 'any' type because @useUnknownInCatchVariables: falsename;}try{// ...}catch({ name } : unknown){^^^^Property'name'doesnotexistontype`{}`// { name } has explicit type 'unknown'name;}

Is this the expected behavior? Or do we need to cover other cases?

@jakebailey

Jake Bailey (jakebailey) commented Feb 1, 2022

Copy link
Copy Markdown
Member

Unless I'm mistaken, all of the examples above should have name be of type any (versus unknown), but that's what the PR has (so maybe your comment is wrong), so that's still good and consistent with other restructuring.

It looks like things are still not quite right in the editor, though. If you use your test case but don't reference the variable in question, it still doesn't show an error. Compare the three cases here, each with the destructured variable left referenced.

const{ xyz }: unknown={};functionfoo({ name }: unknown){// ...}try{// ...}catch({ name }: unknown){// ...}

image

I'm not sure what you've changed to get further is correct, though; it still feels to me like there's something missing during the checker walk, and these errors are only functioning due to the side effect of evaluating the variable later.

Comment threadsrc/compiler/checker.ts Outdated
Comment threadtests/baselines/reference/catchClauseWithTypeAnnotation.errors.txt Outdated
Comment threadsrc/compiler/checker.ts Outdated
Comment threadtests/baselines/reference/destructuringCatch.symbols
@sandersn

Copy link
Copy Markdown
Member

I'm confused about the state of this PR. Why is it marked draft? If a team member signs off, is it OK to merge? If so, I guess that means it needs another review?

@a-tarasyuk

Oleksandr Tarasiuk (a-tarasyuk) commented Apr 1, 2022

Copy link
Copy Markdown
ContributorAuthor

Nathan Shively-Sanders (@sandersn) I'm really sorry for the confusion with the state of the PR. I've closed it to make this issue open to PR acceptance.

@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Milestone BugPRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Destructuring of unknown catch variable is not an error

5 participants

@a-tarasyuk@jakebailey@sandersn@RyanCavanaugh@typescript-bot