以下是type alias部分中官方文档的示例。
type LinkedList<T> = T & { next: LinkedList<T> };
interface Person {
name: string;
}
var people: LinkedList<Person>;
var s = people.name;
//var s = people.next.name;
//var s = people.next.next.name;
//var s = people.next.next.next.name;
当我尝试这个例子时,它会产生以下错误:
TypeError: Cannot read property 'name' of undefined
人是linkList类型。如何正确填写人员链接列表?
答案 0 :(得分:2)
你应该为它赋值,只是声明类型是不够的。例如:
var people: LinkedList<Person> = {name: "Alf", next: {name: "Tim", next: null}};