reactjs - 如何修复绑定(bind)元素 'children' 隐式具有 'any' type.ts(7031)?

标签 reactjs typescript

我在验证中遗漏了一些东西如何添加类型验证?出现错误“元素‘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/

相关文章:

angularjs - 变量 'Foo' 在声明之前使用。两个类(Class)相互依存

reactjs - 如何 react "fetch"

reactjs - 在没有 GitHub fetch 的情况下为 Nextjs : Error: Cannot find module 'sharp' 安装sharp

javascript - 套接字在Flux单向数据流中的适合位置是什么?

reactjs - 当多个请求发出时如何处理刷新 token ?

Angular 5,具有 3 种状态的 Angular Material Checkbox(选中、未选中、不确定)

javascript - Angular 默认下拉值 2 向绑定(bind)

javascript - 使用react、redux和react-router-redux路由到另一个页面之前的动画

typescript - Typescript 中带有 bool 值的可区分联合

python - 无法读取 null 的属性 'data ' (DASH)