努力讓 reduce 函式與 typescript 一起作業——型別和回傳值——從故事書中省略了一些控制元件(在標記為 ** ERROR ** 的代碼中添加了兩個 TS 錯誤)
誰能建議正確的解決方案以及我如何擺脫這些訊息?
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
}
},
}
uj5u.com熱心網友回復:
的型別引數reduce用于指示回傳型別
您想要回傳如下結構:
[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,
},
},
}),
{}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533731.html
上一篇:如何每2個字符拆分字串
