我有这个React组件:
interface ClockState { date: Date }
class Clock extends React.Component<{}, ClockState> {
timerID: number;
constructor(props) {
super(props);
this.state = { date: new Date() };
}
componentDidMount() {
this.timerID = setInterval(() => this.tick(), 1000);
// (TS) Type 'Timer' is not assignable to type 'number'
}
...
}
这可以编译并正常运行,但是我在编辑器中的上方看到一个错误,您可以在注释中看到该错误,在this.timerID
下带有红色波浪状。
如果我将TimerID的类型更改为NodeJS.Timer
,这似乎会让编辑器(Visual Studio)满意,但是现在编译器会抱怨:
TS2322: Type 'number' is not assignable to type 'Timer'.
这两个显然之一是错误的。 这些是我的依赖项,如果有帮助的话:
"devDependencies": {
"aspnet-webpack": "^3.0.0",
"@babel/core": "^7.0.0-rc.1",
"@babel/preset-env": "^7.0.0-rc.1",
"babel-loader": "^8.0.0-beta.4",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"typescript": "^3.0.1",
"ts-loader": "^4.4.2",
"clean-webpack-plugin": "^0.1.19",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"@types/react": "^16.4.11",
"@types/react-dom": "^16.0.7",
"jquery": "^3.3.1",
"react": "^16.4.2",
"react-dom": "^16.4.2"
}