Reputation: 24858
class X {}
let x : X = 123 // why is this ok?
console.log(x instanceof X)
The above code compiles (for some reason) and yields false
.
Please explain to me why TypeScript does not enforce type-correctness in this case.
Edit for future readers:
Murat K's answer is correct but I'd like to save you a click and 15 minutes of your time:
123
is being treated as an object of type Number
.Number
is X
because it has all the methods and fields of X
and more.Upvotes: 2
Views: 63
Reputation: 37614
The empty class is basically an empty object since it has nothing to classify as something. That's why you can assign to it anything.
See the github issue for the same question here
Upvotes: 3