解决函数中"never"错误的正确方法是什么?



我一直在尝试解决打字问题很长一段时间。我有一个函数,它创建一个对象的副本并展开其中一个数组。但这里出现了错误

TS2345: 'TFootnote'类型的实参不能赋值给'never'类型的形参。

push(脚注) 我不知道他到底不喜欢什么。我将感激你的帮助。
export type TCellData = {
unitName: string;
decimals: number;
domain: string;
fieldX: string;
fieldY: string;
footnotes: TFootnote[] | [];
};
export type TFootnote = {
item1: string;
item2: string;
};
export const justFunction = (
obj: TCellData,
footnote: TFootnote
) => {
const newObj = obj;
newObj?.footnotes?.push(footnote);

return newObj;
};

@jcal给出了正确答案。一开始整个设计都是错误的。非常感谢他的帮助!

export type TCellData = {
unitName: string;
decimals: number;
domain: string;
fieldX: string;
fieldY: string;
footnotes: TFootnote[];
};
export type TFootnote = {
item1: string;
item2: string;
};
export const justFunction = (
obj: TCellData,
footnote: TFootnote
): TCellData => ({ ...obj, footnotes: [...obj.footnotes, footnote] });

相关内容

最新更新