foo具有bar类型值,而bar具有foo类型值作为进餐,这表明存在错误
[ts]声明之前使用的块范围变量'foo'。 [2448]
const bar = {
meal: foo, // ¯\_(ツ)_/¯!
sweet: true,
sassy: false,
};
const foo = {
bear : 0,
vodka : false,
redwine: -1,
taste: [bar]
};
答案 0 :(得分:1)
这与任何类型都没有关系,只需要在构造完一个对象后将其添加到另一个对象即可。
const foo = {
bear: 0,
vodka: false,
redwine: -1,
taste: []
};
const bar = {
meal: foo,
sweet: true,
sassy: false,
};
foo.taste.push(bar);
答案 1 :(得分:0)
尝试一下:
const bar = {
sweet: true,
sassy: false,
};
const foo = {
bear : 0,
vodka : false,
redwine: -1,
taste: []
};
bar.meal = foo;
foo.taste[0] = bar;
编辑1:使代码与打字系统兼容
type barType = {
sweet: boolean,
sassy: boolean,
meal: fooType
};
type fooType = {
bear: number,
vodka: boolean,
redwine: number,
taste: barType[]
};
const foo: fooType = {
bear : 0,
vodka : false,
redwine: -1,
taste: []
};
const bar: barType = {
sweet: true,
sassy: false,
meal: foo
};
foo.taste[0] = bar;