Closed
Description
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.