thwd
thwd

Reputation: 24858

Why does this even compile?

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:

Upvotes: 2

Views: 63

Answers (1)

Murat Karagöz
Murat Karagöz

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

Related Questions