Skip to content

"unknown" type testing doesn't work in unreachable code. #47343

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

Closed
mpawelski opened this issue Jan 7, 2022 · 2 comments
Closed

"unknown" type testing doesn't work in unreachable code. #47343

mpawelski opened this issue Jan 7, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mpawelski
Copy link

Bug Report

πŸ”Ž Search Terms

unknown allowUnreachableCode

πŸ•— Version & Regression Information

4.5.4 and 4.6.0-dev.20220107

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function errorHandler2(error:unknown){
  return;
  if(error instanceof Error){
    console.log(error.message); //Error: Object is of type 'unknown'.
  }
}

function errorHandler3(error:unknown){
  throw error
  if(error instanceof Error){
    console.log(error.message); //Error: Object is of type 'unknown'.
  }
}

πŸ™ Actual behavior

I've put return or throw statement just for debugging purpose to end function earlier. And I got compiler error in later code.

πŸ™‚ Expected behavior

No error because I have allowUnreachableCode: true in tsconfig (default) and the unreachable code is valid (but compiler says it is not).

Small bug IMO, nothing serious, but it's something I didn't expect.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 7, 2022

Duplicate of #26914.

allowUnreachableCode causes unreachable code itself to not error. But due to the linked bug the type checking does not work as expected within unreachable code, resulting in a type error.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 11, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants