TypeScript: why is a number assignable to a reference of type Object?

瘦欲@ 提交于 2019-11-28 07:34:49

问题


Why is this legal TypeScript?

var x: number = 5
var y: Object = x

Surely a number is not an Object. One might suspect that x is implicitly coerced (auto-boxed) to an object, but no:

if (!(y instanceof Object)) {
   console.log(typeof y)
}

prints

number

For the record:

$ tsc --version
Version 1.8.10

回答1:


Type compatibility in TypeScript is based on structural subtyping, not nominal typing. That said, consider the two following interface definitions:

interface IFoo { X: number }
interface IBar { X: number; Y: number }

Does IBar extend IFoo? No.

But is IFoo compatible with IBar? Yes.

The members of IFoo are a subset of IBar members, thus you can assign any IBar to IFoo. But it doesn't work the other way around:

var x: IFoo;
var y: IBar;

x = y // all good
y = x // type error, x has no Y member

This way in Typescript all types are compatible with Object if you think of it as the empty interface. This way you can pass any valid typescript value to functions accepting Object and play well with the way Javascript libs are written.

I suggest reading Type Compatibility in docs and the last paragraph about Subtype vs Assignment.



来源:https://stackoverflow.com/questions/39562391/typescript-why-is-a-number-assignable-to-a-reference-of-type-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!