我正在使用 Prismic、Gatsby 和 Typescript 构建一个站点。Prismic 的部分吸引力在于切片功能,它允许您创建更易于内容编辑器使用的动态内容部分。我有一个称为SliceZone
映射页面上所有切片的组件:
切片区.tsx
import React from 'react';
import { ImageCta } from 'components/slices/call-to-action/image/ImageCta';
import { Columns } from 'components/slices/columns/Columns';
import { ContentBlock } from 'components/slices/content-block/ContentBlock';
import { Embed } from 'components/slices/embed/Embed';
import { TextHero } from 'components/slices/hero/text/TextHero';
import { Slider } from 'components/slices/slider/Slider';
import { Video } from 'components/slices/video/Video';
interface PageSlicesProps {
slices: any;
}
const PageSlices = ({ slices }: PageSlicesProps) => {
const sliceComponents = {
hero: TextHero,
slider: Slider,
content_block: ContentBlock,
video: Video,
columns: Columns,
embed: Embed,
image_call_to_action: ImageCta,
};
return slices.map((slice: any, index: number) => {
const SliceComponent = sliceComponents[slice.type];
if (SliceComponent) {
return <SliceComponent slice={slice} key={`slice-${slice.type}-${index}`} />;
}
});
};
interface SliceZoneProps {
bodyContent: any;
}
export const SliceZone = ({ bodyContent }: SliceZoneProps) => <PageSlices slices={bodyContent.body} />;
我需要适当地输入所有内容。但是,我收到此错误SliceComponent
,特别是sliceComponents[slice.type]
:
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ hero: ({ slice }: any) => Element; slider: ({ slice }: any) => Element; content_block: ({ slice }: any) => Element; video: ({ slice }: any) => Element; columns: ({ slice }: any) => Element; embed: ({ slice }: { ...; }) => Element; image_call_to_action: ({ slice }: any) => Element; }'.
我如何创建一个interface
可以适当输入这个的?如果我的问题有点含糊,我很抱歉,我是 React 和 Typescript 的新手,所以语言对我来说有点过时了。