Skip to content

"This condition will always return 'false'" but returns true at runtime #26592

Closed
@ghost

Description

TypeScript Version: 3.1.0-dev.20180818

Code

function sanityCheck(s: string): string {
	return s == 0 ? "I must be going insane" : "I'm fine";
}
console.log(sanityCheck(""));

Expected behavior:

Error message is 100% accurate.

Actual behavior:

src/a.ts:2:9 - error TS2367: This condition will always return 'false' since the types 'string' and 'number' have no overlap.

The == operator in JavaScript converts its arguments -- some developers apparently rely on this behavior (not naming names but I noticed this in real JavaScript code that I pasted into a TypeScript file).
The error message might recommend using an explicit conversion such as Number(s) to get this to compile in TypeScript.

CC @DanielRosenwasser

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messaging

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions