typescript 打字稿原始类型:“数字”和“数字”类型之间有什么区别(TSC 不区分大小写)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15487220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript primitive types: any difference between the types "number" and "Number" (is TSC case-insensitive)?
提问by Cesco
I meant to write a parameter of type number
, but I misspelled the type, writing Number
instead.
我想写一个 type 的参数number
,但我拼错了类型,Number
而是写了。
On my IDE (JetBrains WebStorm) the type Number
is written with the same color that is used for the primitive type number
, while if I write a name of a class (known or unknown) it uses a different color, so I guess that somehow it recognizes the misspelled type as a correct/almost-correct/sort-of-correct type.
在我的 IDE (JetBrains WebStorm) 上,类型Number
用与原始类型相同的颜色编写number
,而如果我写一个类的名称(已知或未知),它使用不同的颜色,所以我猜它以某种方式识别拼写错误的类型为正确/几乎正确/排序正确的类型。
When I compile the code, instead of complaining for example that the compiler couldn't found a class named Number
, TSC writes this error message:
当我编译代码时Number
,TSC并没有抱怨编译器找不到名为 的类,而是编写了以下错误消息:
Illegal property access
Does that mean that number
and Number
both co-exists as different types?
这是否意味着,number
和Number
两者共同存在不同类型的?
If this is true, which is the difference between those classes?
如果这是真的,这些类之间的区别是什么?
If this is not the case, then why it simply didn't write the same error message it displays for unknown classes ("The name 'Number' does not exist in the current scope")
如果不是这种情况,那么为什么它根本没有编写为未知类显示的相同错误消息(“当前范围中不存在名称'Number'”)
This is the code:
这是代码:
class Test
{
private myArray:string[] = ["Hyman", "Jill", "John", "Joe", "Jeff"];
// THIS WORKS
public getValue(index:number):string
{
return this.myArray[index];
}
// THIS DOESN'T WORK: ILLEGAL PROPERTY ACCESS
public getAnotherValue(index:Number):string
{
return this.myArray[index];
}
}
采纳答案by Ryan Cavanaugh
JavaScript has the notion of primitivetypes (number, string, etc) and objecttypes (Number, String, etc, which are manifest at runtime). TypeScript types number
and Number
refer to them, respectively. JavaScript will usually coerce an object type to its primitive equivalent, or vice versa:
JavaScript 有原始类型(数字、字符串等)和对象类型(数字、字符串等,它们在运行时显示)的概念。TypeScript 类型number
并Number
分别引用它们。JavaScript 通常会将一个对象类型强制转换为其原始等价物,反之亦然:
var x = new Number(34);
> undefined
x
> Number {}
x + 1
> 35
The TypeScript type system rules deal with this (spec section 3.7) like this:
TypeScript 类型系统规则是这样处理的(规范第 3.7 节):
For purposes of determining subtype, supertype, and assignment compatibility relationships, the Number, Boolean, and String primitive types are treated as object types with the same properties as the ‘Number', ‘Boolean', and ‘String' interfaces respectively.
为了确定子类型、超类型和赋值兼容性关系,Number、Boolean 和 String 基元类型分别被视为与“Number”、“Boolean”和“String”接口具有相同属性的对象类型。
回答by Shaun Luttin
To augment Ryan's answer with guidance from the TypeScript Do's and Don'ts:
在TypeScript 的注意事项和注意事项的指导下增加 Ryan 的答案:
Don'tever use the types
Number
,String
,Boolean
,Symbol
, orObject
These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code./* WRONG */ function reverse(s: String): String;
Douse the types
number
,string
,boolean
, andsymbol
./* OK */ function reverse(s: string): string;
永远不要使用类型
Number
、String
、Boolean
、Symbol
或Object
这些类型是指在 JavaScript 代码中几乎从未正确使用的非原始装箱对象。/* WRONG */ function reverse(s: String): String;
不要使用类型
number
,string
,boolean
,和symbol
。/* OK */ function reverse(s: string): string;
回答by Ali Adravi
As the TypeScript doc says:
正如 TypeScript 文档所说:
var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
表示任何种类的数字的对象。所有 JavaScript 数字都是 64 位浮点数。
As it says, take any
as parameter and return number or null
正如它所说,any
作为参数并返回数字或null
It give an easy way to check a value is number or not
它提供了一种检查值是否为数字的简单方法
Number("1234"); // 1234
Number("1234.54") // 1234.54
Number("-1234.54") // -1234.54
Number("1234.54.33") // null
Number("any-non-numeric") // null
So simply we can use to check the number, like:
因此,我们可以简单地使用来检查数字,例如:
if(Number(val)){
console.log('val is a number');
} else {
console.log('Not a number');
}