admin管理员组文章数量:1307742
The expression 1==2
causes TypeScript to give the error Operator '==' cannot be applied to types '1' and '2'
. What are the reasons for TypeScript treating these values as being of a different type (the typeof
operator, rather predictably, says that they're both number
s)? Is it a design decision to specifically apply this to numbers, or a byproduct of the overall typing system? What harm could be caused by allowing parison of literals?
The expression 1==2
causes TypeScript to give the error Operator '==' cannot be applied to types '1' and '2'
. What are the reasons for TypeScript treating these values as being of a different type (the typeof
operator, rather predictably, says that they're both number
s)? Is it a design decision to specifically apply this to numbers, or a byproduct of the overall typing system? What harm could be caused by allowing parison of literals?
- 1 thats just because the numbers have different values, it's not a type problem – kevinSpaceyIsKeyserSöze Commented Aug 31, 2017 at 11:29
-
3
But then surely the expression would just return
false
, rather than causing the script to not pile? – clb Commented Aug 31, 2017 at 11:33 - Well, it does pile, just with a warning… but yes, interesting question. Here's a link to reproduce: typescriptlang/play/#src=1%20%3D%3D%202%3B – deceze ♦ Commented Aug 31, 2017 at 11:33
- 2 @ZevSpitz "Why" is not really relevant to the question at hand. I'm asking about the design decision. – clb Commented Aug 31, 2017 at 11:35
-
2
@deceze there are no warnings, that is an error, but the process will still emit the js, unless the
--noEmitOnError
flag is used – Nitzan Tomer Commented Aug 31, 2017 at 11:36
3 Answers
Reset to default 71
and 2
in this context are considered so called literal numbers. It means that value 1
has type 1
and thus can only be 1
, same with 2
. Given that, the expression of 1==2
doesn't make sense because 1
can never be 2
, or more precisely their types mismatch, you can't pare apples to oranges.
Here are the rationale and in depth details on where literals types are assumed by default:
- https://github./Microsoft/TypeScript/pull/10676
About literal types:
- https://github./Microsoft/TypeScript/pull/9407
- https://github./Microsoft/TypeScript/pull/5185
One of many examples as to why literal types are useful:
- https://github./Microsoft/TypeScript/pull/9163
When TypeScript peforms type inference on the expression 1
, it gives it the type 1
, not the type number
. You can see this if you examine code like this:
const a = 1;
If you use your IDE to query the inferred type of a
, you'll see that the type of a is 1
. In the TypeScript playground, for instance, you get a tooltip that says const a: 1
.
So in if (1 == 2)
, 1
has type 1
and 2
has type 2
. TypeScript does not allow you to pare them because they are of different inferred types. This is part of the type safety TypeScript gives you.
You can work around it with:
if (1 as number == 2) {
}
And you mentioned in a ment that you were doing the 1 == 2
parison because you could not do if (false) { ... }
due to the piler plaining about unreachable code. I can work around that problem with this:
if (false as boolean) {
console.log("something");
}
Typescript can create a type from any constant value. This when bined with union types creates a very powerful way of expressing what a function takes as an argument for example:
function doStuff(p : "yes"| 1| true| "no"| 0| false ){
}
doStuff("maybe"); //Error
doStuff(3); // Error
doStuff(1) ; //OK
You are experiencing the unfortunate side effect that errors such as yours instead of being reports as expression is always false
turn into type patibility errors instead.
本文标签: javascriptWhy does TypeScript consider each number its own typeStack Overflow
版权声明:本文标题:javascript - Why does TypeScript consider each number its own type? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741840004a2400443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论