Typescript 中的字符串或数字值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39467714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:50:27  来源:igfitidea点击:

String or Number values in Typescript

javascripttypescript

提问by Tomas

An example of what I am trying to achieve:

我试图实现的一个例子:

class Test {
    private _folderId: number;
    private _pageId: number;
    private _folderName: string;
    private _pageName: string;

    constructor(pageId: string | number, folderId: string | number){
        this._folderId = (!isNaN(+folderId)) ? folderId : undefined;
        this._pageId = (!isNaN(+pageId)) ? pageId : undefined;
        this._folderName = (isNaN(+folderId)) ? folderId : undefined;
        this._pageName = (isNaN(+pageId)) ? pageId : undefined;
    }
}

Unfortunately this throws compiler error:

不幸的是,这会引发编译器错误:

TS2322:Type 'string | number' is not assignable to type 'number'. Type 'string' is not assignable to type 'number'.

and so on (similar error for each var).

依此类推(每个 var 都有类似的错误)。

Is there any way around it? At the moment only thing I can do is to set page and folder id to type any...

有什么办法可以解决吗?目前我唯一能做的就是设置页面和文件夹 ID 以输入任何...

采纳答案by Paleo

The comment under the Guenter Guckelsberger's answer suggests you need to use strings like '123'as numbers. Here is a solution:

Guenter Guckelsberger 的回答下的评论表明您需要将字符串'123'用作数字。这是一个解决方案:

/**
* See http://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers
*/
function isNumeric(n: any) : n is number | string {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function intVal(n: number | string): number {
    return typeof n === "number" ? n : parseInt(n, 10);
}

class Test {
    private _folderId: number;
    private _pageId: number;
    private _folderName: string;
    private _pageName: string;

    constructor(pageId: string | number, folderId: string | number) {
        if (isNumeric(folderId))
            this._folderId = intVal(folderId);
        else
            this._folderName = <string>folderId;
        if (isNumeric(pageId))
            this._pageId = intVal(pageId);
        else
            this._pageName = <string>pageId;
    }
}

回答by Nahush Farkande

Since you have mentioned an ambiguous type for pageIdand folderIdtypescript cannot make out whether your variable is a number or a string at the time of assignment. Hence you need to specify the exact type of your variable at the time of assignment. This can be done using typecasting

由于您提到了一个不明确的类型,pageId并且folderId打字稿在赋值时无法确定您的变量是数字还是字符串。因此,您需要在赋值时指定变量的确切类型。这可以使用类型转换来完成

You can typecast your folderIdand pageIdto number or string as follows:

您可以将您的folderIdand类型转换pageId为数字或字符串,如下所示:

constructor(pageId: string | number, folderId: string | number){
    this._folderId = (!isNaN(+folderId)) ? folderId as number : undefined;
    this._pageId = (!isNaN(+pageId)) ? pageId as number : undefined;
    this._folderName = (isNaN(+folderId)) ? folderId as string : undefined;
    this._pageName = (isNaN(+pageId)) ? pageId as string: undefined;
}

Another way of typecasting would be <number>folderId<string>folderId. You can also go for type Guardsas shown in 'Paleo's answer

另一种类型转换方法是<number>folderId<string>folderId. 您也可以选择“Paleo 的回答”中所示的类型 Guards

回答by Guenter Guckelsberger

You should use the typeof operator like

您应该使用 typeof 运算符,例如

typeof folderId === "number"

to check for number or string.

检查数字或字符串。