Skip to content

Inequality comparison with string literal shows editor error #14983

Closed

Description

@yiochen

I think this is a bug. I might be wrong.
TypeScript Version: 2.2.1

Code

class Greeter {
    greeting: string;
    constructor(message: string) {
        if (message !== 'world' || message !== 'me') {
            // do something
        }
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

Expected behavior:
I am trying this on typescript playground, it should show no error in the editor.
Actual behavior:
The second condition in the if statement is underlined. The error message is

Operator '!==' cannot be applied to types '"world"' and '"me"'.

Same thing happened in Visual studio code with Typescript 2.2.1

Activity

  1. RyanCavanaugh commented on Apr 3, 2017

    @RyanCavanaugh
    Member

    The condition you have here

    if (message !== 'world' || message !== 'me') {

    always evaluates to true.

    Perhaps you meant to write

    if (message !== 'world' && message !== 'me') {
                            ~~

    or

    if (message === 'world' || message === 'me') {
  2. yiochen commented on Apr 3, 2017

    @yiochen
    Author

    Oh, it's because of that. I wish the error message was more intuitive. But thanks, I'm going to close this issue.

  3. locked and limited conversation to collaborators on Jun 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions