我正在使用Meteor和React构建一个应用程序,而Typescript正在向我发出错误的错误:
属性'gameId'在类型'IntrinsicAttributes& {}& {children?:ReactNode; }
我有一个组件应用程序,它呈现组件游戏,如下所示:
render() {
return (
<div className="container">
{this.state.gameId ? <Game gameId={this.state.gameId} /> : this.renderNewGameButtons()}
</div>
);
}
Game
是React.Component
的扩展,定义如下。如您所见,我已将gameId
定义为GameProps
界面中的道具。为什么我仍然收到此错误?
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);
答案 0 :(得分:2)
我认为问题来自您使用gameContainer: any
。 TS不知道你的模块导出了什么,当然不是Game
类,所以你试图渲染它时会出错。我假设createContainer
是一个HOC,正确键入是很棘手但你可以在那里找到例子,如Redux connect
。否则你可以使用断言修复它:
export default createContainer(
// ...
) as React.ComponentClass<GameProps>;
或者,如果这不起作用,请尝试:
export default createContainer(
// ...
) as any as typeof Game;
答案 1 :(得分:0)
我之前遇到过类似的问题,首先验证文件类型是 .tsx。如果那不起作用。检查您是否已保存更改。就我而言,我对几个文件进行了大量更改,但没有保存我修改的所有文件,保存所有文件后错误消失了。