Typescript: reduce object of objects



努力得到一个减少函数与typescript工作-类型和返回值-从故事书中省略了一些控件(在代码中添加了两个TS错误标记** ERROR **)

谁能告诉我正确的解决方案,以及我如何摆脱这些消息?

const controlsToOmit: string[] = [
'connectedLeft',
'connectedRight',
];
interface Props {
accumulator: {
[key: string]: {
table: {
disable: true;
};
};
};
value: string;
}
const controlsToOmitArgTypes = controlsToOmit.reduce<Props>(
(accumulator, value) => ({
...accumulator,
[value]: {
table: {
disable: true,
},
},
}),
{} ** Argument of type '{}' is not assignable to parameter of type 'Props' **
);
export default {
title: 'Components/Buttons/ButtonMeta',
component: ButtonMetaComponent,
argTypes: {
...controlsToOmitArgTypes, ** Spread types may only be created from object types. **
},
};

controlsToOmitArgTypes返回以下对象

{
"connectedLeft": {
"table": {
"disable": true
}
},
"connectedRight": {
"table": {
"disable": true
}
},
}

reduce的type参数用于指示返回类型

你想返回这样的结构:

[key: string]: {
table: {
disable: true;
};
};
const controlsToOmit: string[] = [
'connectedLeft',
'connectedRight',
];
interface Props {
[key: string]: {
table: {
disable: true;
};
};
}
const controlsToOmitArgTypes = controlsToOmit.reduce<Props>(
(accumulator, value) => ({
...accumulator,
[value]: {
table: {
disable: true,
},
},
}),
{}
);

最新更新