【问题标题】:'T' could be instantiated with an arbitrary type which could be unrelated'T' 可以用可能不相关的任意类型实例化
【发布时间】:2021-10-26 04:02:03
【问题描述】:

我必须对数据结构进行练习,特别是使用双向链表,但我不明白为什么当我尝试将两个对象添加到列表时出现此错误:

“'{ id_user: number; id_touristic_place: number; review_title: string; review_desc: string; review_points: number; }'类型的参数不能分配给'T'类型的参数。 'T' 可以用与 '{ id_user: number; 无关的任意类型实例化。 id_touristic_place:号码;评论标题:字符串; review_desc:字符串; review_points:数字; }'.ts(2345)"

我从 TS 的文档中试过这个

public addAtEnd<T>(data: T): void

但这给了我另一个与节点相关的错误

如果您能尝试向我解释错误的原因,我将不胜感激。 (我是 TS 新手)

这是我的代码:

export class Node<T> {
  public data: T;
  public next: Node<T> | null;
  public prev: Node<T> | null;

  constructor(data: T) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

import { Node } from "./Node";
import { ILinkedList } from "./ILinkedList";

export class LinkedList<T> implements ILinkedList<T> {
  private head: Node<T> | null;

  constructor() {
    this.head = null;
  }

  public get gethead(): Node<T> | null {
    return this.head;
  }

  public set sethead(head: Node<T>) {
    this.head = head;
  }

  public addAtEnd(data: T): void {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
    } else {
      const getLast = (node: Node<T>): Node<T> => {
        return node.next ? getLast(node.next) : node;
      };

      const lastNode = getLast(this.head);
      newNode.prev = lastNode;
      lastNode.next = newNode;
    }
  }

  public addTwo(): void {
    this.addAtEnd({
      id_user: 1,
      id_touristic_place: 11,
      review_title: "Nice place",
      review_desc: "C:",
      review_points: 0,
    });

    this.addAtEnd({
      id_user: 2,
      id_touristic_place: 11,
      review_title: "Good",
      review_desc: "Not bad at all  ",
      review_points: 0,
    });
  }
}

【问题讨论】:

    标签: node.js typescript generics


    【解决方案1】:

    您的 LinkedList 类是通用的,这意味着您不能假设它包含任何特定类型的数据。

    如果您想在列表中存储某种类型的数据,例如Users,您需要指定该类型。这通常通过构造LinkedList&lt;User&gt; 的实例来完成。

    您也可以创建一个类 ListOfUsers extends LinkedList&lt;User&gt;,但这只有在存在适用于用户列表但不适用于任何其他数据类型列表的功能时才有用。

    例如:

    export class LinkedList<T> implements ILinkedList<T> {
      private head: Node<T> | null;
    
      constructor() {
        this.head = null;
      }
    
      public get gethead(): Node<T> | null {
        return this.head;
      }
    
      public set sethead(head: Node<T>) {
        this.head = head;
      }
    
      public addAtEnd(data: T): void {
        const newNode = new Node(data);
        if (!this.head) {
          this.head = newNode;
        } else {
          const getLast = (node: Node<T>): Node<T> => {
            return node.next ? getLast(node.next) : node;
          };
    
          const lastNode = getLast(this.head);
          newNode.prev = lastNode;
          lastNode.next = newNode;
        }
      }
    }
    
    interface User {
          id_user: number;
          id_touristic_place: number;
          review_title: string;
          review_desc: string;
          review_points: number;
    };
    
    const myList = new LinkedList<User>();
    myList.addAtEnd({
      id_user: 1,
      id_touristic_place: 11,
      review_title: "Nice place",
      review_desc: "C:",
      review_points: 0,
    });
    myList.addAtEnd({
      id_user: 2,
      id_touristic_place: 11,
      review_title: "Good",
      review_desc: "Not bad at all  ",
      review_points: 0,
    });
    

    【讨论】:

      【解决方案2】:

      它告诉你,你在这一行传入的对象不可能满足所有可能的类型T

      this.addAtEnd({
        id_user: 1,
        id_touristic_place: 11,
        review_title: "Nice place",
        review_desc: "C:",
        review_points: 0,
      });
      

      作为无限可能之一,如果Tnumber,那么传入number(如this.addAtEnd(4))是合法的,但您的代码不会传入@ 987654327@.

      泛型的目的是您的代码可以与 any 类型一起使用,因此您无法对该类型做出任何假设,除非您对其添加限制。对于您的addTwo 函数,您将无法创建一个与所有可能的T 匹配的临时对象,因此您可以做的唯一实际的事情就是让它接受数据作为参数。这样,使用此类的任何人都可以提供他们想要的类型的数据。

      public addTwo(first: T, second: T): void {
        this.addAtEnd(first);
        this.addAtEnd(second);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2021-01-19
        • 2021-05-11
        • 2020-11-25
        相关资源
        最近更新 更多