Skip to content
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

String literal type comparison should result in an error if the compared value is invalid #7112

Closed
NoelAbrahams opened this issue Feb 17, 2016 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@NoelAbrahams
Copy link

Hi,

We have been adding compile time safety to the magic string problem through the excellent string literal type introduced in 1.8.

One case that we are finding slipping through the safety net is the following:

type Foo = 'a' | 'b';

const foo: Foo = 'a';

if (foo === 'bar') {
}

Here the Foo type is being compared to an incompatible value 'bar'.

It would be nice to get an error here similar to an enum comparison error:

enum Foo { a, b }
enum Bar { a, b }
let foo: Foo;

if (foo === Bar.a) { // Operator '===' cannot be applied to types 'Foo' and 'Bar'
}

This is especially useful for obtaining a compile time error when the original strings ('a' | 'b') are refactored during the course of development.

I'm aware that there are workarounds involving pseudo string enums, but this is a suggestion for this primitive case.

Thanks.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 17, 2016
@DanielRosenwasser
Copy link
Member

This looks like a duplicate of #6149. The general issue is discussed at #6167. A fix exists at #6196.

@RyanCavanaugh
Copy link
Member

This has been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants