I want to write a class like this:
class Foo {
public someProp = '123';
}
but I mistyped and wrote this:
class Foo{
public someProp: '123'; // not "="
}
I expect getting a compilation error but nothing happens. Why is it so?
Because TypeScript supports constatnts as type when you need to list allowed values in the field. It is not a bug. It is feature. :)
var x: '123';
var y: '123' | '456';
x = '123';
x = '456'; // Error
x = '789'; // Error
y = '123';
y = '456';
y = '789'; // Error
see TypeScript Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With