LondonBoy
LondonBoy

Reputation: 149

Transpiling error in Typescript with React: Property does not exist on type 'IntrinsicAttributes'

I am building an app with Meteor and React and Typescript is throwing me a transpiling error:

Property 'gameId' does not exist on type 'IntrinsicAttributes & {} & { children?: ReactNode; }

I have a component App which renders a component Game, as so:

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

Game is an extension of React.Component, and is defined as below. As you can see, I have defined gameId as a prop in the GameProps interface. Why am I still receiving this error?

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);

Upvotes: 5

Views: 5395

Answers (2)

Abdul-Razak Adam
Abdul-Razak Adam

Reputation: 1128

I faced similar issues before, first verify the file type is .tsx. If that doesn't work. Check that you have saved your changes. In my case, I made lots of changes to several files and I did not save all the files I modified, the error went away when I saved all the files.

Upvotes: 0

Aaron Beall
Aaron Beall

Reputation: 52153

I believe the issue comes from your use of gameContainer: any. TS doesn't know what your module exports, certainly not a Game class, so you get an error trying to render it. I assume createContainer is a HOC, which is tricky to properly type but you can find examples out there, like Redux connect. Otherwise you could probably fix it using an assertion:

export default createContainer(
    // ... 
) as React.ComponentClass<GameProps>;

Or, if that doesn't work, try this:

export default createContainer(
    // ... 
) as any as typeof Game;

Upvotes: 5

Related Questions