Skip to content

Unable to use !== on union types with a string literal types #11277

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
e98cuenc opened this issue Sep 30, 2016 · 5 comments
Closed

Unable to use !== on union types with a string literal types #11277

e98cuenc opened this issue Sep 30, 2016 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@e98cuenc
Copy link

TypeScript Version: 2.0.3

Code

class Color {
    constructor(public r: number, public g: number, public b: number) {
    }
}

var f: 'none' | Color = new Color(0, 0, 0);
f !== 'none';

Expected behavior:
No errors

Actual behavior:
error TS2365: Operator '!==' cannot be applied to types 'Color' and 'string'.

If the tested variable is a member of an object, the above code works:

class Color {
    constructor(public r: number, public g: number, public b: number) {}
}

var f: { x: 'none' | Color } = { x: new Color(0, 0, 0) }; 
f.x !== 'none';
@ahejlsberg
Copy link
Member

ahejlsberg commented Sep 30, 2016

This is working as intended. The error in the first example is reported because the control flow analyzer knows that the actual type of f in that location is Color (due to the assignment right above) and a Color value is not comparable to a string value (effectively the comparison doesn't make sense). Contrast that with this example

function foo(f: 'none' | Color) {
    f !== 'none';  // Ok
}

Here the comparison is ok because the control flow analyzer only knows the declared type for f, so f might be the string literal 'none'. The same is true in your second example (although it is arguable we could know that f.x is a Color, we only track direct assignments to f.x).

If you really want a 'none' value that you can always compare to a 'none' | Color, you can explicitly give it that type in a type annotation:

const None: 'none' | Color = 'none';

Alternatively, you can use the expression 'none' as ('none' | Color).

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 30, 2016
@normalser
Copy link

@ahejlsberg Isn't it the wrong assumption of control flow analyzer about actual type of f in that location ?

Consider:

class Color {
    constructor(public r: number, public g: number, public b: number) {
    }
}

var f: 'none' | Color = new Color(0, 0, 0);
function test() {
    f = 'none'
}
test()
f !== 'none'; 

@ahejlsberg
Copy link
Member

@wallverb Effects of assignments in nested functions is an orthogonal issue. We currently don't account for them, and given the first class nature of functions we'll never be able to completely analyze such side effects without being overly conservative. There have been many discussions on this topic and #9998 summarizes where we are.

@normalser
Copy link

Thank you @ahejlsberg . Great discussion

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Oct 8, 2016

This feature is catching common logic errors and forcing people to write cleaner code that has fewer redundant conditionals. It's absolutely fantastic.
Found a bug today that was due to writing || instead of &&. I have a feeling a lot of languages are going to start adopting these kinds of features over the next few years ❤️

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants