【发布时间】:2018-07-10 12:33:30
【问题描述】:
我浏览了TS handbook,来到了我认为非常有吸引力的类型别名概念。然后我尝试运行这段代码sn-p:
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;
什么给了(TS v2.9.1):error TS2454: Variable 'people' is used before being assigned.
所以我初始化people如下:
var people: LinkedList<Person> =
{name: "John", next: {name: "Jannet", next: {name: "Joanna", next: {name: "Adam", next: undefined}}}};
var s = people.name;
var s = people.next.name;
var s = people.next.next.name;
var s = people.next.next.next.name;
但现在我收到 TSError:
error TS2322: Type '{ name: string; next: { name: string; next: { name: string; next: { name: string; next: undefined...' is not assignable to type 'LinkedList<Person>'.
我在哪里做错了?
【问题讨论】:
标签: typescript type-alias