Skip to content

False in logical conjunction causes missed null check #40093

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
joarwilk opened this issue Aug 17, 2020 · 1 comment
Closed

False in logical conjunction causes missed null check #40093

joarwilk opened this issue Aug 17, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@joarwilk
Copy link

TypeScript Version: 3.9.2

Search Terms: false logical conjunction null

Expected behavior: Both statements should be valid

Actual behavior: One statement causes a typescript error

Code

const str: string | null = Math.random() > 0.5 ? "Hello World" : null;

const isfalse = false;

false && str && console.log(str.length) // Object (str) is possibly 'null'.
isfalse && str && console.log(str.length) // Valid
Output
"use strict";
const str = Math.random() > 0.5 ? "Hello World" : null;
const isfalse = false;
false && str && console.log(str.length);
isfalse && str && console.log(str.length);
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Not the worst bug in the world and not very relevant for production scenarios, but interesting nonetheless.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 17, 2020
@RyanCavanaugh
Copy link
Member

Duplicate #26914

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

2 participants