[英]What is the correct way to solve the "never" error in a function?
很长一段时间以来,我一直在尝试用打字稿解决这个问题。 我有一个创建对象副本并扩展其中一个数组的函数。 但在这里我得到一个错误
TS2345:“TFootnote”类型的参数不可分配给“从不”类型的参数。
错误出现在此位置 ...push(footnote)
我无法弄清楚他到底不喜欢什么。 我将不胜感激。
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;
};
@jcals 给出了正确答案。 整个设计一开始是错误的。 非常感谢他的帮助!
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] });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.