【问题标题】:Type '{}' is not assignable to type 'IntrinsicAttributes & DropdownProps & Props &类型“{}”不可分配给类型“IntrinsicAttributes & DropdownProps & Props &
【发布时间】:2019-05-12 07:29:34
【问题描述】:

我正在使用 React on Rails |打包机 |打字稿:“^2.8.1”。

我有一个高阶组件,它接受一个组件并在其中注入一些道具,然后返回它:

export type HCProps = {
  isValid: boolean,
};

然后我有一个组件,它从高阶组件中获取注入的道具:

import injector, { HCProps } from './HCComponent';
type Props = {};

const LocalComponent: React.SFC<Props & HCProps> = ({ isValid }) => (

);

export default injector(LocalComponent);

这里的LocalComponent在渲染为组件时没有像DOM属性那样接受任何props,但是props是由HCComponent注入的:

<LocalComponent />

这给了我错误:

Type '{}' is not assignable to type 'IntrinsicAttributes & HCProps & Props & { children?: ReactNode; }'.
Type '{}' is not assignable to type 'HCProps'.
Property 'isValid' is missing in type '{}'.

知道为什么会这样吗?我是打字稿的新手。

【问题讨论】:

    标签: javascript reactjs typescript react-props


    【解决方案1】:

    我猜你说的时候弄错了

    LocalComponent 在渲染时不接受任何道具

    可能你正在渲染默认导出的组件injector(LocalComponent)

    在这种情况下,您使用的是注入器为您提供的任何组件。注入器中应该有一个泛型类型,您应该使用类似

    injector<HCProps>(LocalComponent)
    

    【讨论】:

    • 嗨,我的意思是我只是把它渲染成&lt;LocalComponent /&gt;,而不是像&lt;LocalComponent isValid={ true } /&gt;。所有的道具都通过高阶组件注入。
    • 不知何故,在我在compilerOptionstsconfig.json 中评论"types": ["node"] 后,错误被删除了。需要确认并阅读此配置选项。
    • 这真的很奇怪 :) 如果出现问题,您可以创建一个示例 repo
    • 是的,我会这样做的。谢谢。
    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 2021-12-03
    • 2018-06-22
    • 2022-06-12
    • 2022-01-18
    • 2019-12-30
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多