简体   繁体   English

条件语句中类型推断的 TS 错误,当条件使用函数调用的结果而不是布尔表达式时

[英]TS Error with Type Inference within Conditional Statement, when the condtional is using the result of a function call instead of a boolean expression

I have the following TypeScript class for a Linked List class, and everything works fine.我有以下用于链接列表类的 TypeScript 类,并且一切正常。

type ListItem = number | string | object;

class Node {
  private value: ListItem;
  private next: Node | null;

  constructor(value: ListItem) {
    this.value = value;
    this.next = null;
  }

  set nodeValue(value: ListItem) {
    this.value = value;
  }

  set nextNode(next: Node | null) {
    this.next = next;
  }

  get nodeValue(): ListItem {
    return this.value;
  }

  get nextNode(): Node | null {
    return this.next;
  }
}

export class LinkedList {
  private head: Node | null;
  private tail: Node | null;

  constructor(value: ListItem | null = null) {
    // Case 1: Linked List is initialised with 1 argument
    // Case 2: Linked List is initialised with null
    if (value) {
      const node = new Node(value);
      this.head = node;
      this.tail = node;
    } else {
      this.head = null;
      this.tail = null;
    }
  }

  public addLast(item: ListItem): void {
    const newNode = new Node(item);

    // Case 1 (if): Empty List
    // Case 2 (else): Non Empty List
    if (this.head === null || this.tail == null) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.nextNode = newNode;
      this.tail = this.tail.nextNode;
    }
  }

  public addFirst(item: ListItem): void {
    const newNode = new Node(item);

    // Case 1 (if): Empty List
    // Case 2 (else): Non Empty List
    if (this.head === null || this.tail === null) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.nextNode = this.head;
      this.head = newNode;
    }
  }
}

Now I thought of creating a helper function, isEmpty(), to check if the Linked List is empty as shown below.现在我想创建一个辅助函数 isEmpty() 来检查链表是否为空,如下所示。

  private isEmpty(): boolean {
    return this.head === null || this.tail === null;
  }

And then changing the addLast() function as follows然后改变 addLast() 函数如下

  public addLast(item: ListItem): void {
    const newNode = new Node(item);

    // Case 1 (if): Empty List
    // Case 2 (else): Non Empty List
    if (this.isEmpty()) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.nextNode = newNode; // error
      this.tail = this.tail.nextNode; // error
    }
  }

But this causes an error, which makes sense because now I guess TS is not aware of the implementation of my conditional, only the result and does not know this.tail or this.head can no longer be null inside the else statement.但这会导致错误,这是有道理的,因为现在我猜 TS 不知道我的条件的实现,只知道结果并且不知道 this.tail 或 this.head 在 else 语句中不能再为空。 Is there any way around this.有没有办法解决。 Could I somehow use my helper without the tsc complaining.我可以在没有 tsc 抱怨的情况下以某种方式使用我的助手吗? I thought of perhaps using a type guard of some sort but couldn't think of something.我想过也许使用某种类型的防护,但想不出什么。 I am still new to TS, is this possible, am I missing something obvious I could do?我还是 TS 新手,这可能吗,我是否遗漏了一些我可以做的明显事情? Or is the helper not a feasible option?或者帮手不是一个可行的选择?

You could use the not null or undefined assertion operator to let the compiler k now you know that tail is assigned at that point.您可以使用 not null 或 undefined 断言运算符让编译器 k 现在您知道在该点分配了 tail 。

this.tail!.nextNode = newNode;
this.tail! = this.tail!.nextNode;

You can find out more here您可以在此处了解更多信息

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

相关问题 typeof 结果,当存储在变量中时,不再被 TS 用于类型推断 - typeof result, when stored in a variable, is not longer used by TS for type inference 使用联合键入(布尔和函数)时出现错误TS2349 - Error TS2349 when using union typing (boolean and function) TS 4.6 上具有推理和扩展子句的条件类型 - Conditional type with inference and extends clause on TS 4.6 使用 require 时出现打字稿错误 - 此表达式不可调用。 类型“typeof import(...)”没有调用签名.ts(2349) - Typescript error when using require - This expression is not callable. Type 'typeof import(...)' has no call signatures.ts(2349) 打字稿:使用条件映射键时获取正确的推断类型 - Typescript: Getting correct inference type when using conditional mapped keys 函数参数类型推断和条件类型 - Function argument type inference and conditional type TS错误:无法调用类型缺少调用签名的表达式 - TS error: Cannot invoke an expression whose type lacks a call signature 为什么ts在声明变量的时候不调用function,提示类型错误 - Why doesn't ts call the function when declaring a variable, indicating a type error 使用参数对 function 进行类型推理 - Type Inference on a function using a parameter TS:跨多个接口的类型推断和 function 签名不推断? - TS: Type inference across multiple interfaces and function signature not inferring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM