【问题标题】:Property is missing in type error类型错误中缺少属性
【发布时间】:2017-02-15 22:43:24
【问题描述】:

我在处理演示 React 项目时遇到问题。大多数问题源于我试图在他们使用 Javascript 的 Typescript 中实现它。这个要点是我的 app.tsx 文件。

https://gist.github.com/jjuel/59eed01cccca0a7b4f486181f2a14c6c

当我运行它时,我收到与界面有关的错误。

./app/app.tsx 中的错误 (38,17):错误 TS2324:“IntrinsicAttributes & IntrinsicClassAttributes & AppProps & { children?: ReactElement |...”类型中缺少属性“humans”。

./app/app.tsx 中的错误 (38,17):错误 TS2324:“IntrinsicAttributes & IntrinsicClassAttributes & AppProps & { children?: ReactElement |...”类型中缺少属性“stores”。 webpack:bundle 现在有效。

我不知道这是怎么回事。有谁知道我的问题可能是什么?谢谢!

【问题讨论】:

  • 404 Page not found...所以这个问题没用。
  • 404 Page not found in the original code :(

标签: reactjs typescript


【解决方案1】:

你有以下界面:

interface AppProps {
    humans: any;
    stores: any;
}

你是说App必须传入humansstores。但是,您在ReactDOM.render(<App />, document.getElementById('main')); 没有属性 中将其初始化为<App />。这是 TypeScript,为您提供有关您所说的意图用途的错误。这是一个错误,就像有一个带参数的函数但在没有传递任何参数的情况下调用它。

修复

也许这些属性对您来说是可选的?如果是这样声明:

interface AppProps {
    humans?: any;
    stores?: any;
}

否则提供它们,例如<App humans={123} stores={123} />(人类和商店的示例实例)。

【讨论】:

  • 将参数设为可选的问题在于,在使用道具的渲染方法中,您会收到参数可能未定义的错误。正如所指出的,这有点像调用没有参数的函数。但是对于函数,使用默认参数。对于 React,在 render 方法中似乎没有考虑 defaultProps?
猜你喜欢
  • 2019-10-30
  • 1970-01-01
  • 2017-11-10
  • 2022-11-16
  • 2020-05-23
  • 1970-01-01
  • 2021-01-05
  • 2020-04-07
  • 1970-01-01
相关资源
最近更新 更多