如何解决打字稿错误TS2339:属性'XXX'在类型'IntrinsicAttributes& ...?

时间:2017-10-04 09:18:14

标签: javascript reactjs typescript

在我的typescript / reactjs应用程序中,我试图传递一个名为test的属性,这是index.tsx的一部分:

ReactDOM.render(
  <Provider store={getStore()}>
    <BrowserRouter>
      <App test={1} />
    </BrowserRouter>
  </Provider>,
  document.getElementById('content')
);

在我的app.tsx中,我有:

export class App extends React.Component<any,{}>{
  constructor(props){
    super(props); 
  }

  render(){
    ..
  }
}

当我运行应用程序时,我收到此错误:

ERROR in ./src/index.tsx
(24,12): error TS2339: Property 'test' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<{}, ComponentState>> & Readonly<{ childr...'.

如何确保解决此错误或如何在App组件上传递属性?

3 个答案:

答案 0 :(得分:1)

对于ReactRedux连接,这似乎是一个更大的问题,而且找到了一个可行的解决方案,我们的团队正在认真考虑在我们的反应应用程序中删除Typescript。

答案 1 :(得分:1)

您需要为组件的interface定义Props,并将其作为类型参数传递给React.Component。这样,App就会收到名为prop的{​​{1}}。例如:

test

围绕interface Props { test: <whatever type test is> } class App extends React.Component<Props, {}> { // your component code } 与TS进行了一些臭名昭着的讨论。您可以查看此SO帖子以获取帮助:Typescript + React/Redux: Property "XXX" does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes

答案 2 :(得分:0)

确保组件名称以大写字母开头

相关问题