gpt4 book ai didi

javascript - 类型中缺少属性 'children'

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

我正在尝试使用 babel-loader 使用 Typescript 设置 Storybook和 ts-loader .

除了使用 children 外,一​​切正常在 React 组件中:

[tsl] ERROR in .../stories/index.stories.tsx(8,56)
TS2769: No overload matches this call.
Property 'children' is missing in type '{ title: string; }' but required in type 'Props'.

这是 .storybook/main.js文件:
module.exports = {
addons: [
"@storybook/addon-knobs",
],
stories: ["../packages/**/*.stories.tsx"],
webpackFinal: async config => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('ts-loader')
},
{
loader: require.resolve('babel-loader'),
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react"
]
}
}
],
});

config.resolve.extensions.push('.ts', '.tsx');

return config;
}
};

这是 index.stories.tsx文件:
import React from "react";

import Collapsible from "../src";

export default {
title: "Collapsible"
};

const content = <span>Content</span>;

export const simpleCollapsible = () => (
<Collapsible title="Collapsible">{content}</Collapsible>
);

这是Collapsible的实现:
import React, { ReactNode, useState } from "react";
import styled, { ThemeProvider } from "styled-components";
import {
BorderProps,
ColorProps,
FlexboxProps,
LayoutProps,
SpaceProps,
TypographyProps
} from "styled-system";
import Theme from "@atw/theme";

import { KeyboardArrowDown } from "@styled-icons/material";

import Box from '~/box';
import Button from '~/button';

interface Props
extends BorderProps,
ColorProps,
FlexboxProps,
LayoutProps,
SpaceProps,
TypographyProps {
children: ReactNode;
title: string;
}

const Collapsible = ({ children, title, ...rest }: Props) => {
const [isCollapsed, setIsCollapsed] = useState(false);

const handleCollapse = () => {
setIsCollapsed(!isCollapsed);
};

return (
<ThemeProvider theme={Theme}>
<Button
border="none"
padding={0}
marginBottom={2}
width={1}
textAlign="start"
onClick={handleCollapse}
{...rest}
>
<IconBox isCollapsed={isCollapsed}>
<KeyboardArrowDown size="24px" />
</IconBox>
{title}
</Button>
{!isCollapsed && (
<Box display="flex" flexDirection="column">
{children}
</Box>
)}
</ThemeProvider>
);
};

export default Collapsible;

这里有什么我做错了吗?

最佳答案

一种可能的解决方案是利用功能组件中的默认子机制,React.FC ,这使您可以挂载子代,而无需在类型定义中明确将它们作为 Prop 包含在内。对于您的情况,可以通过应用以下更改来实现:

interface Props
extends BorderProps,
ColorProps,
FlexboxProps,
LayoutProps,
SpaceProps,
TypographyProps {
title: string;
}

const Collapsible: React.FC<Props> = ({ children, title, ...rest }) => {
...
};

Working sandbox for this

关于javascript - 类型中缺少属性 'children',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60840693/

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