Gur*_*rdt 8 javascript typescript reactjs
我想做的是循环当前的帖子类型和“产品”,但我正在努力处理这些类型。所以我收到以下错误:
“Record<string,any>[]”类型的参数不可分配给“never”类型的参数。
关于这一部分:
...pages.map((page) => ({
Run Code Online (Sandbox Code Playgroud)
我的代码:
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,
})),
];
Run Code Online (Sandbox Code Playgroud)
添加有效的代码:
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,
}))
: [];
});
Run Code Online (Sandbox Code Playgroud)
这里它针对一种帖子类型,即您当前正在编辑的帖子类型,但我想在某些帖子类型上循环它。
例子:
const selectablePostTypes = ['page', 'post', 'product'];
Run Code Online (Sandbox Code Playgroud)
这是你的初始化postList
const postList = [];
Run Code Online (Sandbox Code Playgroud)
因为您没有为打字稿提供任何值来“找出”应属于该数组的内容的类型签名,所以它将其设置为
never[]
Run Code Online (Sandbox Code Playgroud)
这意味着它禁止您向该空数组添加任何值。在这里添加类型
const postList: Record<string, any>[][] = [];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1931 次 |
| 最近记录: |