你的代码有几个问题,
(name: string)
定义调用签名,可能需要索引签名:
[name: string]
,表示可以使用
[]
任何字符串。
同时,你的对象文字暗示
Room
接口不是由字符串可索引的,而是具有
Movements
字段。
这很管用,应该能让你开始:
interface Outcome {
Difference: string,
Room: string
};
interface Movement {
[name: string]: {
Points: number,
Exits: Array<Outcome>
}
};
interface Room {
Movements: Array<Movement>
};
interface Rooms {
[name: string]: Room
};
interface Definitions {
Rooms: Rooms
};
const defs: Definitions = {
Rooms: {
"Kitchen": {
Movements: [{
"Living Room": {
Points: 3,
Exits: [
{ Difference: "<= 1", Room: "Bedroom" },
{ Difference: "2", Room: "Living Room" },
]
},
Bedroom: {
Points: 2,
Exits: [
{ Difference: "<= 2", Room: "Closet" },
]
},
}]
}
}
}
Playground link