gilamran
gilamran

Reputation: 7294

Why doesn't TypeScript raise an error when I assign a number to an Object?

I would expect an error on this code TypeScript:

var obj:Object;
var num:number;

obj = 4; // no error!
num = 4; // ok
num = obj; // error.

Any good reason for not getting an error?

Upvotes: 2

Views: 478

Answers (1)

David Sherret
David Sherret

Reputation: 106640

Object: Provides functionality common to all JavaScript objects.

The Object object is contained in all other JavaScript objects; all of its methods and properties are available in all other objects. - Source

It works the way you described because a number is an Object, but an Object isn't necessarily a number.

Upvotes: 3

Related Questions