reactjs - React에서 Typescript로 변환 중 오류: 'IntrinsicAttributes' 타입에 속성이 존재하지 않습니다
저는 Meteor와 React로 앱을 만들고 있는데, Typescript가 변환 오류를 발생시키고 있습니다:
속성 'gameId'는 타입 'IntrinsicAttributes & {} & { children?: ReactNode; }'에 존재하지 않습니다
나는 App이라는 컴포넌트를 가지고 있으며, 이 컴포넌트는 다음과 같이 Game 컴포넌트를 렌더링합니다:
render() {
return (
<div className="container">
{this.state.gameId ? <Game gameId={this.state.gameId} /> : this.renderNewGameButtons()}
</div>
);
}
Game는 React.Component를 확장한 것이며, 아래와 같이 정의되어 있습니다. 보시다시피, 저는 GameProps 인터페이스에서 gameId를 prop으로 정의했습니다. 그런데 왜 여전히 이 오류가 발생하는 걸까요?
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);
답변 #1
문제는 당신이 gameContainer: any를 사용한 것에서 비롯된 것이라고 생각합니다. TS는 당신의 모듈이 무엇을 내보내는지 알 수 없고, 특히 Game 클래스도 알 수 없기 때문에 렌더링하려고 할 때 오류가 발생하는 것입니다. 저는 createContainer가 HOC라고 가정하는데, 이것을 제대로 타입 지정하는 것은 까다롭지만, Redux connect 같은 사례를 찾아볼 수 있습니다. 그렇지 않으면 다음과 같은 단언을 사용해서 아마도 해결할 수 있을 것입니다:
export default createContainer(
// ...
) as React.ComponentClass<GameProps>;
또는, 그것이 작동하지 않으면, 이것을 시도해 보세요:
export default createContainer(
// ...
) as any as typeof Game;
답변 #2
저도 이전에 비슷한 문제를 겪었는데, 먼저 파일 형식이 .tsx인지 확인하세요. 그래도 안 되면 변경 사항을 저장했는지 확인하세요. 제 경우에는 여러 파일을 많이 수정했지만 수정한 모든 파일을 저장하지 않았습니다. 모든 파일을 저장했을 때 오류가 사라졌습니다.
