typescript中合并对象的类型应该是什么?
我有 我的最后一个目标我希望它看起来像这样:typescript中合并对象的类型应该是什么?,typescript,Typescript,我有 我的最后一个目标我希望它看起来像这样: export interface Game { startingTime?: string; minutes?: string; finished: boolean; homeTeam: string; awayTeam: string; score?: string; } export class Markets { constructor() { return {
export interface Game {
startingTime?: string;
minutes?: string;
finished: boolean;
homeTeam: string;
awayTeam: string;
score?: string;
}
export class Markets {
constructor() {
return {
'1': '',
x: '',
'2': '',
'1x': '',
x2: ''
};
}
}
那么游戏的类型应该是什么呢?
这并没有让eslint抱怨,但这是正确的吗
const games = [
{...{ finished: true, homeTeam: 'Santa Rosa', awayTeam: 'Molinos El Pirata', score: '0-3' }, ...new Markets()},
{...{ finished: false, minutes: '75', homeTeam: 'Chavelines', awayTeam: 'Deportivo Coopsol', score: '1-0' }, ...new Markets()}
];
对我来说,这看起来更正确:
games: Game | Markets[] = [];
但埃斯林特说:
games: Game & Markets[] = [];
可以将类型括在括号中,以便它们都应用于数组
(游戏与市场)[]
或者像@lleon建议的那样使用Array
。正确的类型应该是Array
我觉得游戏与市场[]也应该起作用:)非常感谢,请添加它作为答案而不是评论,这样我就可以接受了。另外,你也可以使用(游戏与市场)[
@ToddSkelton啊,好了,这样做是可能的,但它需要括号。谢谢!
Type 'never[]' is not assignable to type 'Game & Markets[]'.
Type 'never[]' is missing the following properties from type 'Game': finished, homeTeam, awayTeamts(2322)