1

Struggling a bit getting a reduce function working with typescript - both the types and the return value - omitting some controls from storybook (added two TS errors in the code marked ** ERROR **)

Can anyone advise of the correct solution and how I get rid of the messages?

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. **
  },
};

The controlsToOmitArgTypes returns the following object

{
    "connectedLeft": {
        "table": {
            "disable": true
        }
    },
    "connectedRight": {
        "table": {
            "disable": true
        }
    },
}
5
  • Is your question how to get rid of these warnings? Commented Nov 15, 2022 at 14:00
  • {} as Props is usually a solution Commented Nov 15, 2022 at 14:03
  • Ultimately the correct use of typescript here which would also get rid of the errors. I have achieved getting rid of the warnings by adding reduce(accumulator: Record<string, unknown>, value: string) but it's probably not the best solution? Commented Nov 15, 2022 at 14:04
  • That removes the error @KonradLinkowski but it now type hints for controlsToOmitArgTypes.accumalator/value. Which isn't what I was expecting in my return object (code snippet in my post), is my type wrong from what I expect to return? Commented Nov 15, 2022 at 14:08
  • 1
    The type parameter for reduce is indicating what is the type for the second argument. You used Props so reduce expects that {} is of type Props. .reduce<Record<string, unknown>>( would probably fix the issue without casting Commented Nov 15, 2022 at 14:14

1 Answer 1

3

The type argument of reduce is used to indicate the return type

You want to return a structure like:

[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,
      },
    },
  }),
  {}
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.