typescript 打字稿类型转换不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34032303/
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 type casting not working
提问by Kuan
I am pretty new to TypeScript. I started with a book called Typescript Revealed (Pub Feb.2013). In Chapter 2 there is a section called "Casts" that has the following example:
我对打字稿很陌生。我从一本名为 Typescript Revealed(2013 年 2 月出版)的书开始。在第 2 章中有一个名为“Casts”的部分,其中包含以下示例:
var a : int = <int>SomeNumberAsAString;
var a : int = <int>SomeNumberAsAString;
I tried to apply the example, as follows:
我尝试应用该示例,如下所示:
var SomeNumberAsAString = "1000";
var a: int = <int>SomeNumberAsAString;
But compiler gave me an error:
但是编译器给了我一个错误:
hello.ts(2,8): error TS2304: Cannot find name 'int'.
hello.ts(2,15): error TS2304: Cannot find name 'int'.
hello.ts(2,8): 错误 TS2304: 找不到名称“int”。
hello.ts(2,15): 错误 TS2304: 找不到名称“int”。
I'm wondering how to do this cast, or has the specification of Typescript changed?
我想知道如何进行此转换,或者 Typescript 的规范是否已更改?
回答by basarat
(Pub Feb.2013)
(2013 年 2 月发布)
That book is old. Its called number
now.
那本书是旧的。它number
现在叫。
var SomeNumberAsAString = "1000";
var a: number = <number><any>SomeNumberAsAString;
Also this assertion is very unsafe and I would not do this in production code. But it gets the point across :)
此外,这个断言非常不安全,我不会在生产代码中这样做。但它得到了重点:)
More
更多的
A more up to date book chapter on assertions : https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html
关于断言的最新书籍章节:https: //basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html
回答by shabunc
I've read @basarat's answer and decided to post my own since I strongly believe that there's still some room for explanation.
我已阅读@basarat 的回答并决定发布我自己的回答,因为我坚信仍有一些解释空间。
Be warned, <number><any>
casting won't generate a number. In fact it will allow your code to be compiled (thus you'll pass all static typing checks) but it won't be a number in javascript. Consider this snippet of code:
请注意,<number><any>
投射不会生成数字。事实上,它允许编译您的代码(因此您将通过所有静态类型检查),但它不会是 javascript 中的数字。考虑这个代码片段:
let str = "1000";
let a: number = <number><any>str;
let b: number = parseInt(str); //or just let b = parseInt(str)
console.log(typeof a); // string
console.log(typeof b); // number
I hardly can imagine cases when a
-case is beneficial compared to b
-case. I'd go with just parseInt or parseFloat or Number, whatever fits more. <T><any>
casting looks smart but you must be 100% sure what you are supposed to achieve by that idiom.
我很难想象a
-case 比 -case 更有利的b
情况。我只会使用 parseInt 或 parseFloat 或 Number,无论哪种更适合。<T><any>
铸造看起来很聪明,但你必须 100% 确定你应该通过那个习语实现什么。
And in most cases you probably don't want to achieve that )
在大多数情况下,您可能不想实现这一点)
回答by Lonli-Lokli
I prefer this variant
我更喜欢这个变种
let SomeNumberAsAString = '1000';
let a = +SomeNumberAsAString;
console.log(a);
回答by Ash Blue
Here is the cleanestway to do it.
这是最干净的方法。
const numberString = '1000';
const a: int = numberString as any;
回答by Alex Chaliy
const a: number = <number> +SomeNumberAsAString;
+SomeNumberAsAString
converts the string value to the number.
+SomeNumberAsAString
将字符串值转换为数字。
<number>
before +SomeNumberAsAString
says to Typescript compiler that we need to cast the type of the value to type number
.
<number>
之前+SomeNumberAsAString
对 Typescript 编译器说我们需要将值的类型转换为 type number
。