简体   繁体   English

当我为对象分配数字时,为什么TypeScript不会引发错误?

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

I would expect an error on this code TypeScript : 我希望在此代码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? 有没有收到错误的充分理由?

Object : Provides functionality common to all JavaScript objects. Object :提供所有JavaScript对象共有的功能。

The Object object is contained in all other JavaScript objects; Object对象包含在所有其他JavaScript对象中; 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 . 它按照您描述的方式工作,因为number是一个Object ,但是一个Object不一定是number

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们将原始字符串分配给对象 String 时,为什么 TypeScript 不显示错误? - Why doesn't TypeScript show an error when we assign primitive string to object String? Typescript 在索引类型不匹配时不会引发错误 - Typescript doesn't raise error when index types doesn't match 带有 typescript 的 JSDoc,定义自定义 object 键类型不会引发错误 - JSDoc with typescript, defining custom object key type doesn't raise error TypeScript不会因不兼容的承诺返回而引发错误 - TypeScript doesn't raise error for incompatible promise returns 在导入的对象上运行不存在的方法时,为什么TypeScript没有给我编译时错误? - Why doesn't TypeScript give me a compile time error when running inexistent method on imported object? TypeScript 不分配接口,而分配对象文字 - TypeScript doesn't assign with interface while assign with object literal 为什么在隐式类型的返回对象上返回无效属性时,TypeScript 不会抛出错误? - Why doesn't TypeScript throw an error when returning invalid properties on implicitly typed return object? 为什么在将具有超出属性的对象分配给变量时 TypeScript 不显示错误? - Why doesn't TypeScript show an error when assigning an object with exceeding properties to a variable? 为什么 typescript 不知道我检查了 object 类型? - Why typescript doesn't know that i checked for object type? 为布尔值使用undefined时,为什么TypeScript没有检测到错误? - Why TypeScript doesn't detect error when use undefined for boolean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM