Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript typeof typeguard在分配给变量时不起作用吗?_Javascript_Typescript - Fatal编程技术网

Javascript typeof typeguard在分配给变量时不起作用吗?

Javascript typeof typeguard在分配给变量时不起作用吗?,javascript,typescript,Javascript,Typescript,当我使用下面这样的typeof时,我得到一个错误 function func (variable: number | string) { const is_number = (typeof variable === 'number'); let variable2: number, variable3: string; if (is_number) variable2 = variable; => Type 'str

当我使用下面这样的
typeof
时,我得到一个错误

function func (variable: number | string) {
    const is_number = (typeof variable === 'number');
    let variable2: number,
        variable3: string;

    if (is_number)
        variable2 = variable;
        => Type 'string | number' is not assignable to type 'number'.
           Type 'string' is not assignable to type 'number'.
    else
        variable3 = variable;
        => Type 'string | number' is not assignable to type 'string'.
           Type 'number' is not assignable to type 'string'.
}
function func (variable: number | string) {
    let variable2: number,
        variable3: string;

    if (typeof variable === 'number')
        variable2 = variable;
    else
        variable3 = variable;
}
但是,没有如下所示的错误

function func (variable: number | string) {
    const is_number = (typeof variable === 'number');
    let variable2: number,
        variable3: string;

    if (is_number)
        variable2 = variable;
        => Type 'string | number' is not assignable to type 'number'.
           Type 'string' is not assignable to type 'number'.
    else
        variable3 = variable;
        => Type 'string | number' is not assignable to type 'string'.
           Type 'number' is not assignable to type 'string'.
}
function func (variable: number | string) {
    let variable2: number,
        variable3: string;

    if (typeof variable === 'number')
        variable2 = variable;
    else
        variable3 = variable;
}

我总是要像上面那样使用它吗?还是有我用错的零件

感谢阅读:)

@Ry-已经回答了评论中的实际问题。下面是限制的解决方法

我将调用一个helper函数<;code>isNumber,该函数断言给定参数
是否扩展了typescript中的某个类型。这提供了一种不内联JS领域类型断言的方法,比如
typeof variable==“number”
。相反,断言现在包含在函数中

这是使用
x is y
构造完成的,该构造称为a(您必须向下滚动一点)。在下面的代码段中,
isNumber
的返回类型就是这样一个类型谓词:

const isNumber=(subject:any):subject是number=>{
返回主题的类型=='number';
};
在这里,typescript可以从从
isNumber
返回的布尔值推断类型
number
。现在,如果您在自己的
func
中使用此helper函数,那么一切都应该很好:

function func (variable: number | string) {
    let variable2: number;
    let variable3: string;

    if (isNumber(variable)) {
        variable2 = variable;
    } else {
        variable3 = variable;
    }
}
由于只有当
isNumber(variable)
返回true时才会执行
if
块,因此Typescript现在将假定
主题为number
。以下伪代码演示了TS应如何解释上述代码:

if (variable is number) {
    // ...
}

我还发现这进一步解释了类型谓词构造。

正确,它没有。编译器在识别这些模式方面只能做到这一点。@Ry-您应该回答您的注释。也许可以参考公共关系?我会投赞成票:——)@TitianCernicova Dragomir:什么公关?