简体   繁体   English

TypeScript:转换类型'a | b' 到 'a' 基于 typeof

[英]TypeScript: Convert type 'a | b' to 'a' based on typeof

I'm new to TypeScript and trying to understand how casting can be used.我是 TypeScript 的新手,并试图了解如何使用铸造。 I am working with an API that defines a function similar to the following:我正在使用定义类似于以下内容的 function 的 API :

function foo(a: number): number | undefined;

I want to perform a function on the return value of this number to safely convert it to a number always, but I get an error:我想对该数字的返回值执行 function 以始终安全地将其转换为数字,但出现错误:

function myFunc(bar: number | undefined): number {
  if (typeof bar === undefined) {
    return 0;
  } else {
    return bar; // error ts(2322)
  }
}

Where ts(2322) expands to Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.其中ts(2322)扩展为Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.

I feel like this is something that should be determinable at compile time.我觉得这是应该在编译时确定的。 Clearly, we only go into the else if typeof bar === number , but trying that in this code snippet:显然,我们只将 go 放入 else if typeof bar === number ,但在此代码片段中尝试:

function myFunc(bar: number | undefined): number {
  if (typeof bar === number) { // error ts(2367)
    return bar;
  } else {
    return 0;
  }
}

The error message is This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and 'Requireable<number>' have no overlap.错误消息是This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and 'Requireable<number>' have no overlap. This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and 'Requireable<number>' have no overlap.

At this point I'm pretty lost on what's possible with casting in TypeScript.在这一点上,我对在 TypeScript 中进行投射的可能性感到非常迷茫。 Is there a way to accomplish my goals?有没有办法实现我的目标? Any assistance would be greatly appreciated.任何帮助将不胜感激。

if (typeof bar === undefined) {

The typeof operator produces a string, so what you should really be testing is: typeof 运算符产生一个字符串,所以你真正应该测试的是:

if (typeof bar === "undefined") {

As you've got it, the if case will never be true, so the code always goes into the else block.如您所知, if情况永远不会成立,因此代码总是进入else块。 Typescript thus deduces that the return statement can be reached with bar still undefined, which conflicts with the type you said you'd be returning. Typescript 因此推断返回语句可以在bar仍未定义的情况下到达,这与您说要返回的类型冲突。

Nicholas gives the detailed explanation, but you could certainly implement your function simply as: Nicholas 给出了详细的解释,但您当然可以简单地实现您的 function:

function myFunc(bar: number | undefined): number {
    return bar || 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM