使用 React 在 Typescript 中转换错误:“IntrinsicAttributes”类型上不存在属性

Lon*_*Boy 4 meteor typescript reactjs

我正在使用 Meteor 和 React 构建一个应用程序,Typescript 向我抛出了一个编译错误:

属性 'gameId' 不存在于类型 'IntrinsicAttributes & {} & { children?: ReactNode; }

我有一个组件应用程序,它呈现一个组件游戏,如下所示:

render() {
    return (
       <div className="container">
          {this.state.gameId ? <Game gameId={this.state.gameId} /> : this.renderNewGameButtons()}
       </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

Game是 的扩展React.Component,定义如下。如您所见,我gameIdGameProps界面中定义了一个道具。为什么我仍然收到此错误?

interface GameProps {
  game?: any,
  gameId?: string,
  subscriptionLoading?: boolean,
}

interface GameState {
    isAscending: boolean,
}

class Game extends React.Component<GameProps, GameState> {
  constructor() {
    super();
    this.state = {
      isAscending: true,
    }
  }
  updateGame(game) {
    Meteor.call('games.update', this.props.gameId, game.history, game.xIsNext, game.stepNumber);
  }
  handleClick(i) {
    const history = this.props.game.history.slice(0, this.props.game.stepNumber+1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.props.game.xIsNext ? 'X' : 'O';

    this.props.game.history = history.concat([{
      squares: squares
    }]);

    this.props.game.xIsNext = !this.props.game.xIsNext;
    this.props.game.stepNumber = history.length;

    this.updateGame(this.props.game);
  }
  jumpTo(step) {
    this.props.game.stepNumber = step;
    this.props.game.xIsNext = (step % 2) ? false : true;

    this.updateGame(this.props.game);
  }

  resortMovesList() {
    this.setState({
      isAscending: !this.state.isAscending,
    })
  }

  render() {
    if (this.props.subscriptionLoading) {
      return <div>Game is loading.</div>
    };

    const history = this.props.game.history;
    const current = history[this.props.game.stepNumber];
    const winner = calculateWinner(current.squares);

    let status;
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status = "Next player: " + (this.props.game.xIsNext? 'X' : 'O');
    }

    const moves = history.map((step, move) => {
      if (!this.state.isAscending) {
        move = history.length - move - 1;
      }
      const desc = move ?
        'Move #' + move :
        'Game start';
      return (
        <li key={move} className={move === this.props.game.stepNumber ? 'current-move' : ''}>
          <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
        </li>
      );
    });

    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={(i) => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <ol>{moves}</ol>
          <button onClick={() => this.resortMovesList()}>
            {this.state.isAscending ? 'Sort Descending' : 'Sort Ascending'}
          </button>
        </div>
      </div>
    );
  }
}

let gameContainer: any;

export default gameContainer = createContainer(props => {
  const gamesSubscription = Meteor.subscribe('games');
  const subscriptionLoading = !gamesSubscription.ready();
  const game = Games.findOne(props.gameId);

  return {
    subscriptionLoading,
    game,
  };
}, Game);
Run Code Online (Sandbox Code Playgroud)

Aar*_*all 5

我相信问题来自您使用gameContainer: any. TS 不知道你的模块导出什么,当然不是一个Game类,所以你在尝试渲染它时会出错。我假设createContainer是 HOC,很难正确输入,但您可以在那里找到示例,例如Reduxconnect。否则,您可能可以使用断言修复它:

export default createContainer(
    // ... 
) as React.ComponentClass<GameProps>;
Run Code Online (Sandbox Code Playgroud)

或者,如果这不起作用,请尝试以下操作:

export default createContainer(
    // ... 
) as any as typeof Game;
Run Code Online (Sandbox Code Playgroud)