報錯如下:
Type '{ navList: any; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Nav> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Property 'navList' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Nav> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
錯誤代碼:
import React from 'react';
class Nav extends React.PureComponent<{
},{
}> {
constructor(props) {
super(props);
this.state = {
};
}
render () {
const navList = this.props && this.props.navList || {};
return (
<div>
</div>
)
}
}
export default Nav;
問題是我們在這里把 props描述成了一個空對象。

改正之后:
import React from 'react';
class Nav extends React.PureComponent<{
navList?: any
},{
}> {
constructor(props) {
super(props);
this.state = {
};
}
render () {
const navList = this.props && this.props.navList || {};
return (
<div>
</div>
)
}
}
export default Nav;
注:state 也同樣存在這個問題,詳見:http://www.itdecent.cn/p/a2f45dc7d45c