2016-09-22 90 views
0

以下是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 

人來說是一個鏈表類型。如何正確填寫人員鏈接列表?

回答

2

你應該給它賦值,只聲明類型是不夠的。例如:

var people: LinkedList<Person> = {name: "Alf", next: {name: "Tim", next: null}};