【发布时间】:2018-06-15 03:56:10
【问题描述】:
我尽量缩短我的代码以消除混乱,实际上我正在尝试将基于 React jsx 构建的应用程序转换为 React Typescript(tsx 文件)。
我收到的错误是 - '[ts] 属性'state' 在类型'App' 上不存在。'[ts] 属性也一样'setState' 在类型 'App'.any' 上不存在 请帮我解决这个问题...
interface STATE {
loading: boolean
};
interface PROPS {};
export default class App extends Component<STATE, PROPS> {
constructor(props:any) {
super(props);
this.state = {
fruitsData : [],
loading: false
};
}
componentDidMount() {
this.setState({
loading: true
});
//Further functions present here plus call to service and binding the
data received to the array fruitsData
}
我的 package.json
{
"name": "example",
"version": "0.1.0",
"private": true,
"devDependencies": {
"@types/classnames": "^2.2.3",
"@types/node": "^4.0.35",
"classnames": "^2.2.5",
"gh-pages": "^0.12.0",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-scripts": "0.9.5",
"typescript": "^2.7.0-insiders.20171214"
},
"dependencies": {
"@types/react": "^16.0.34",
"@types/react-dom": "^16.0.3",
"awesome-typescript-loader": "^3.4.1",
"react-search-box": "0.0.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
}
【问题讨论】:
-
你确实为 react 和 ReactDOM 安装了 @types 对吗?
-
@Sujit.Warrier 抱歉,还没有,@types/react-dom 和 @types/react 安装正在进行中
-
不幸的是,我不确定我们是否有足够的资源来重现您的问题。如果您安装了必要的模块,那么我猜这个问题可能来自 TypeScript 配置不正确。你是从 create-react-app 开始的吗?如果是这样,有使用 TypeScript 的指南。
标签: reactjs typescript react-native typescript-typings