Typescript:减少对象的对象

u3r8eeie  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(213)

在获取处理typescript的reduce函数时遇到了一些困难-类型和返回值-省略了故事书中的一些控件(在标记为ERROR的代码中添加了两个TS错误)
有人能告诉我正确的解决方案吗?我如何删除这些消息?

  1. const controlsToOmit: string[] = [
  2. 'connectedLeft',
  3. 'connectedRight',
  4. ];
  5. interface Props {
  6. accumulator: {
  7. [key: string]: {
  8. table: {
  9. disable: true;
  10. };
  11. };
  12. };
  13. value: string;
  14. }
  15. const controlsToOmitArgTypes = controlsToOmit.reduce<Props>(
  16. (accumulator, value) => ({
  17. ...accumulator,
  18. [value]: {
  19. table: {
  20. disable: true,
  21. },
  22. },
  23. }),
  24. {} ** Argument of type '{}' is not assignable to parameter of type 'Props' **
  25. );
  26. export default {
  27. title: 'Components/Buttons/ButtonMeta',
  28. component: ButtonMetaComponent,
  29. argTypes: {
  30. ...controlsToOmitArgTypes, ** Spread types may only be created from object types. **
  31. },
  32. };

controlsToOmitArgTypes返回以下对象

  1. {
  2. "connectedLeft": {
  3. "table": {
  4. "disable": true
  5. }
  6. },
  7. "connectedRight": {
  8. "table": {
  9. "disable": true
  10. }
  11. },
  12. }
2vuwiymt

2vuwiymt1#

reduce的类型参数用于指示返回类型
您需要返回如下结构:
第一个

相关问题