gpt4 book ai didi

reactjs - TS2339 : Property X does not exist on type '{}'

转载 作者:搜寻专家 更新时间:2023-10-30 21:05:14 24 4
gpt4 key购买 nike

我似乎无法解决这个错误。

ERROR in App.tsx(9,15) TS2339: Property 'count' does not exist on type '{}'.

App.tsx

import React from "react";

import Test from "./Test";

const App = () => {
return (
<div>
<Test count={1} />
</div>
);
}

export default App;

测试.tsx

import React from "react";

interface Props{
count: number
}
interface State{}

class Test extends React.Component<Props, State>{
render(){
return <div>{this.props.count}</div>;
}
}

export default Test;

我的tsconfig如下,

{
"compilerOptions": {
"allowJs": true,
"jsx": "react",
"module": "es6",
"target": "es5",
"allowSyntheticDefaultImports": true
},
"include": ["src"]
}

我也在使用 webpack 来构建应用程序。我正在使用 ts-loader

加载 typescript

package.json

  "dependencies": {
"axios": "^0.18.0",
"express": "^4.16.3",
"fetch": "^1.1.0",
"less": "^3.8.0",
"mathjax": "^2.7.5",
"node-html-parser": "^1.1.8",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"webpack": "^4.16.3"
},
"devDependencies": {
"@types/express": "^4.16.0",
"classnames": "^2.2.6",
"css-loader": "^1.0.0",
"less-loader": "^4.1.0",
"nodemon": "^1.18.3",
"style-loader": "^0.21.0",
"ts-loader": "^4.4.2",
"ts-node": "^7.0.0",
"typescript": "^3.0.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}

谢谢!

最佳答案

安装 React 类型定义:

npm install -D @types/react @types/react-dom

如果没有安装类型,TS 无法正确推断 JSX 元素所需的属性。希望他们将来能让错误更清楚一些 c:

关于reactjs - TS2339 : Property X does not exist on type '{}' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51681529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com