简体   繁体   English

如何使用null ts类型检查解决问题?

[英]How to fix problem with null ts type checking?

I have property of class that can be null and in code I'm checking if value not null and is array - push new value to it, but type checker still thinks that value can be null. 我有可以为null的类的属性,并且在代码中我正在检查值是否不为null且为数组-向其推送新值,但类型检查器仍认为该值可以为null。 Can somebody explain why and how to fix it, thanks? 有人可以解释为什么以及如何解决它,谢谢?

class BIT {
  head: BIT | null = this;
  private value: string;
  right: BIT | BIT[] | null = null;
  left: BIT | BIT[] | null = null;

  somefunction(node: BIT) {
    const haveLeft = this.left !== null;
    const leftIsBranch = haveLeft && this.left instanceof Array;

    if (haveLeft) {
      if (leftIsBranch) {
        this.left.push(node);
      }
    }
  }
}

UPD: if I get rid of boolean variables haveLeft and leftIsBranch and explicitly add it to if statement -> everything work just fine. UPD:如果我摆脱了布尔变量haveLeft和leftIsBranch,并将其显式添加到if语句中->一切正常。 What the heck is going on? 到底他妈发生了什么?

class BIT {
  head: BIT | null = this;
  private value: string;
  right: BIT | BIT[] | null = null;
  left: BIT | BIT[] | null = null; // below you try push: this.left.push(this.value). But your types it's BIT or Array of BIT or null, when 'this.value' is string. 

  somefunction() {
    const haveLeft = this.left !== null;
    const leftIsBranch = haveLeft && this.left instanceof Array;

    if (haveLeft) {
      if (leftIsBranch) {
        this.left.push(value); // you forgot to specify 'this': this.left.push(this.value);
      }
    }
  }
}

Also instead of example: null | BIT 也代替example: null | BIT example: null | BIT you can specify example?: BIT example: null | BIT可以指定example?: BIT

In TypeScript all types are nullable by default: 在TypeScript中,所有类型默认情况下都可以为空:

By default null and undefined are subtypes of all other types. 默认情况下,null和undefined是所有其他类型的子类型。 That means you can assign null and undefined to something like number. 这意味着您可以将null和undefined分配给数字。

However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types. 但是,使用--strictNullChecks标志时,只能将null和undefined分配给void及其各自的类型。 This helps avoid many common errors. 这有助于避免许多常见错误。 In cases where you want to pass in either a string or null or undefined, you can use the union type string | 如果要传递字符串或null或未定义,则可以使用联合类型string |。 null | 空| undefined. 未定义。 Once again, more on union types later on. 再一次,稍后会更多地讨论联合类型。

[From TS docs ] [摘自TS docs ]

So unless you're using the --strictNullChecks compiler flag, adding | null 因此,除非您使用--strictNullChecks编译器标志,否则添加| null | null is not necessary. 不需要| null

The cause of your type check error is probably that you're checking against null but not undefined - which is the default value of uninitialized fields. 类型检查错误的原因可能是您要检查null而不是undefined这是未初始化字段的默认值。 A loose equality ( != instead of !== ) check should help identifying also undefined cases: 松散的等式( !=代替!== )检查应有助于识别未定义的情况:

const haveLeft = this.left != null; // This also excludes `undefined`

Note the following type checking. 请注意以下类型检查。

 console.log(typeof null); // object console.log(Array.isArray([])); // true 

暂无
暂无

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

相关问题 Typescript 类型检查错误如何在 React 中修复 - Typescript type checking error how to fix in React 使用 jest 和 History 时如何修复 TS 错误“缺少类型”? - How to fix TS error 'type is missing' when working with jest and History? 为什么运行时类型检查在 ts 中如此重要? - Why is runtime type checking so important in ts? (TS - Redux)如何将 state 属性初始化为 null:类型“UserData”不可分配给类型“null” - (TS - Redux) How to initialize state property as null: Type 'UserData' is not assignable to type 'null' 如何修复'Typescript“正在进行类型检查......”花费太长时间'? - How to fix 'Typescript “Type checking in progress…” taking too long'? JS 到 TS:类型“null”不可分配给类型“数字” - JS to TS: Type 'null' is not assignable to type 'number' 量角器打字稿在tsConfig文件中将ts转换为js文件时出现问题。 我该如何解决? - Protractor Typescript Problem converting ts to js file in tsConfig file. How do I fix this? 如何解决“无法从 null 读取属性“1.0””jQuery 问题? - How to fix 'Cannot read property “1.0” from null' jQuery problem? 如何解决'属性'宽度'在类型'HTMLElement'上不存在。' 当使用// @ ts-check在vscode中键入检查Javascript(NOT Typescript)时? - How do I solve 'Property 'width' does not exist on type 'HTMLElement'.' when type checking Javascript (NOT Typescript) using // @ts-check in vscode? 如何修复找不到模块“./aws-export”或其对应的类型 declarations.ts (2307) - How to fix Cannot find module './aws-export' or its corresponding type declarations.ts (2307)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM