简体   繁体   English

类型 'undefined' 不能分配给类型 'number' .ts(2322)

[英]Type 'undefined' is not assignable to type 'number' .ts(2322)

I'm stuck in learning Typescript language and need some explanations.我一直在学习 Typescript 语言,需要一些解释。 The problem is that variable named as this.value is never assigned as undefined due isValid function check it.问题是命名为this.value变量从未被分配为 undefined 由于isValid函数检查它。 How to make typescript understand it?如何让打字稿理解它?

export const isValid = (n: any) => n && n > 0 && n < 10;

class Test {
    value: number;
    constructor(value?: number) {
        /*
        Type 'number | undefined' is not assignable to type 'number'.
        Type 'undefined' is not assignable to type 'number'.ts(2322)
        */
        this.value = isValid(value) ? value : -1;
    }
}

As the value in constructor is optional.因为构造函数中的value是可选的。 its type is number | undefined它的类型是number | undefined number | undefined , you need to cast it as number when you are assigning it: number | undefined ,您需要在分配时将其转换为数字:

 this.value = isValid(value) ? value as number : -1 ;

By default, the type checker does not look at the implementation of called functions, only their signature.默认情况下,类型检查器不查看被调用函数的实现,只查看它们的签名。 Therefore, the typechecker for the constructor does not know that isValid will only return true if n is a number.因此,构造函数的类型检查器不知道isValid只会在n是数字时返回 true。

You can either inline the code of isValid into the constructor:您可以将isValid的代码内联到构造函数中:

constructor(value) {
  this.value = value && value > 0 && value < 10 ? value : -1;
}

or extend the function signature of isValid with a user defined type guard :或使用用户定义的类型保护扩展isValid的函数签名:

export function isValid(n: any): n is number {
  return n && n > 0 && n < 10;
}

暂无
暂无

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

相关问题 键入'字符串 | 号码 | boolean' 不能分配给类型 'undefined'。 类型“字符串”不可分配给类型“未定义”.ts(2322) - Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322) 类型“编号”不可分配给类型“PersonConfig”。 ts(2322) - Type 'number' is not assignable to type 'PersonConfig'. ts(2322) 类型 &#39;(activeTabIndex: number) =&gt; void&#39; 不可分配给类型 &#39;() =&gt; number&#39;.ts(2322) - Type '(activeTabIndex: number) => void' is not assignable to type '() => number'.ts(2322) TS2322:类型“属性”不可分配给类型“布尔值 | 不明确的'。 切换 - TS2322: Type 'Atrribute' is not assignable to type 'boolean | undefined'. Toggle 类型“未知”不可分配给类型“从不”.ts(2322) - Type 'unknown' is not assignable to type 'never'.ts(2322) 类型“元素”不可分配给类型“字符串”.ts(2322) - Type 'Element' is not assignable to type 'string'.ts(2322) 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined' TS2322:输入&#39;Promise <Hero | undefined> &#39;不可分配给&#39;Promise <Hero> “ - TS2322: Type 'Promise<Hero | undefined>' is not assignable to type 'Promise<Hero>' Typescript React Native AnimatedEvent:“类型‘number’不可分配给类型‘Mapping’.ts(2322)” - Typescript React Native AnimatedEvent: 'Type 'number' is not assignable to type 'Mapping'.ts(2322)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM