我在验证中遗漏了一些东西如何添加类型验证?出现错误“元素‘children’隐式具有‘any’类型”。
import * as React from 'react';
import Button from './Styles';
const Button1 = ({ children, ...props }) => (
<Button {...props}>{children}</Button>
);
Button1.propTypes = {};
export default Button1;
最佳答案
编辑 2022: 用react 18,FC不再提供children了,所以要自己敲,可以drop FC:
import React, { ReactNode } from "react";
interface Props {
children?: ReactNode
// any props that come into the component
}
const Button1 = ({ children, ...props }: Props) => (
<Button {...props}>{children}</Button>
);
是的,您缺少一个整体的 Props 类型,这意味着 typescript 将其视为 any
而您的 ts 规则不允许它。
您必须将 Prop 输入为:
import React, { FC } from "react";
interface Props {
// any props that come into the component
}
const Button1: FC<Props> = ({ children, ...props }) => (
<Button {...props}>{children}</Button>
);
关于reactjs - 如何修复绑定(bind)元素 'children' 隐式具有 'any' type.ts(7031)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370851/