gpt4 book ai didi

javascript - react 类型错误 "not assignable to parameter of type ' never'"

转载 作者:行者123 更新时间:2023-12-05 00:26:55 24 4
gpt4 key购买 nike

我想要做的是遍历当前的 posttype 和“产品”,但我正在努力处理这些类型。所以我收到以下错误:

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


在这部分:
...pages.map((page) => ({
我的代码:
  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,
})),
];
添加有效的代码:
  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,
}))
: [];
});
这里它针对一种帖子类型,即您当前正在编辑的帖子类型,但我想在一些帖子类型上循环。
例子:
const selectablePostTypes = ['page', 'post', 'product'];

最佳答案

这是您对 postList 的初始化

const postList = [];
因为您没有为 typescript 提供任何值来“找出”应该属于该数组的内容的类型签名,所以它将其设置为
never[]
这意味着它禁止您向该空数组添加任何值。在此处添加类型
const postList: Record<string, any>[][] = [];

关于javascript - react 类型错误 "not assignable to parameter of type ' never'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69220780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com