Skip to content

assert.strictEqual(false, ...) triggers unreachable code error #38699

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
alexander-fenster opened this issue May 21, 2020 · 3 comments · Fixed by #38710
Closed

assert.strictEqual(false, ...) triggers unreachable code error #38699

alexander-fenster opened this issue May 21, 2020 · 3 comments · Fixed by #38710
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@alexander-fenster
Copy link

alexander-fenster commented May 21, 2020

assert.strictEqual(false, anyArgument) triggers unreachable code error for any code below after the recent @types/node update. This only reproduces with @types/node 10.17.24, 12.12.41, 13.13.8 (published recently).

Note that it only reproduces with false as a value being checked. Replace it with anything else and it will compile.

TypeScript Version: 3.9.3, 3.8.x as well

Search Terms: strictEqual error TS7027: Unreachable code detected

Code

import * as assert from 'assert';

const b = false;
assert.strictEqual(false, b);
console.log('really unreachable?');

Expected behavior: should compile with --allowUnreachableCode false.

Actual behavior:

$ npx tsc --allowUnreachableCode false src/index.ts 
src/index.ts:5:1 - error TS7027: Unreachable code detected.

5 console.log('really unreachable?');
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

Playground Link: N/A because of the specific version of @types/node required for a repro.

Related Issues: N/A

Background: Here is the commit in @types/node that apparently triggers this: DefinitelyTyped/DefinitelyTyped@e910379
I reported it there as well in DefinitelyTyped/DefinitelyTyped#44944, but I have an impression the compiler behaves weirdly here.

@G-Rath
Copy link

G-Rath commented May 21, 2020

Cross-posting from the DT issue for coverage:

This does look like it might be a bug in tsc, as while true & false are given a bit of extra special treatment (iirc), it still fails if both parameters are the false literal which doesn't seem right.

However, you can work around it in the meantime by casting false to boolean:

import * as assert from 'assert';

const b = false;

assert.strictEqual(false as boolean, b);
console.log('really unreachable?');

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 9, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Jun 9, 2020
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Jun 21, 2020
@ahejlsberg
Copy link
Member

This is a bug in our code reachability analysis. It's a simple fix. Will put up a PR.

@ahejlsberg
Copy link
Member

Ah, we already have a fix in #38710, but it never got merged. Will do now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
4 participants