类型别名中的打字稿类型交集
问题描述:
以下是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
人来说是一个链表类型。如何正确填写人员链接列表?
答
你应该给它赋值,只声明类型是不够的。例如:
var people: LinkedList<Person> = {name: "Alf", next: {name: "Tim", next: null}};