#StackBounty: #javascript #reactjs #typescript React type error "not assignable to parameter of type 'never'"

Bounty: 50

What I want to do is loop over current posttype and ‘products’ but I’m always struggle with the types. So I got the following error:

Argument of type ‘Record<string, any>[]’ is not assignable to parameter of type ‘never’.

On part this part:

...pages.map((page) => ({

Here is my code:

    const pages = useSelect((select) => {
    const editor = select('core/editor');
    const currentPostType: string = editor.getCurrentPostType();
    const selectablePostTypes = [currentPostType, 'products'];

    const postList = [];

    selectablePostTypes.forEach((singlePostType) => {
      const records: Record<string, any>[] = select('core').getEntityRecords(
        'postType',
        singlePostType,
      );

      postList.push(records);
    });
  });

  // Make dropdown of pagesOptions
  const pagesOptions = [
    ...[
      {
        value: '0',
        label: __('No page selected', 'demosite'),
      },
    ],
    ...pages.map((page) => ({
      value: page.id,
      label: page.title,
    })),
  ];

Add the code part which works:

      const pages = useSelect((select) => {
    const editor = select('core/editor');
    const postType: string = editor.getCurrentPostType();
    const records: Record<string, any>[] = select('core').getEntityRecords(
      'postType',
      postType,
    );

    return records ?
      records.map((record) => ({
        description: record.description,
        id: record.id,
        featuredMedia: record.featuredMedia,
        link: record.link,
        subtitle: record.subtitle,
        title: record.title.rendered,
      })) :
      [];
  });

But here it is targeted on one post type, the post-type your editing right now. But I want to loop this over some posttypes.

examples:

const selectablePostTypes = ['page', 'post', 'product'];


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.