Property 'hoverIndex' does not exist on type 'Readonly<{}>'

在使用 react + typeScript 过程中遇到这样的一个错误: Property 'hoverIndex' does not exist on type 'Readonly<{}>'.

错误代码如下:

import React from 'react';

class Nav extends React.PureComponent<{
  navList?: any
},{
  
}> {
  constructor(props) {
    super(props);
    this.state = {
      hoverIndex: 999
    };
  }

  render () {
    const hoverIndex = this.state && this.state.hoverIndex || 999;
    return (
      
) } }

问题是我们在这里把 state描述成了一个空对象。

改正之后:

import React from 'react';

class Nav extends React.PureComponent<{
  navList?: any
},{
  hoverIndex?: number  
}> {
  constructor(props) {
    super(props);
    this.state = {
      hoverIndex: 999
    };
  }

  render () {
    const hoverIndex = this.state && this.state.hoverIndex || 999;
    return (
      
) } }

你可能感兴趣的:(Property 'hoverIndex' does not exist on type 'Readonly<{}>')