我是TypeScript的新手.我在渲染方法中显示this.state.something或将其分配给函数内的变量时出现问题.
看看最重要的一段代码:
interface State {
playOrPause?: string;
}
class Player extends React.Component {
constructor() {
super();
this.state = {
playOrPause: 'Play'
};
}
render() {
return(
<div>
<button
ref={playPause => this.playPause = playPause}
title={this.state.playOrPause} // in this line I get an error
>
Play
</button>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
错误说:"[ts]属性'playOrPause'在'ReadOnly <{}>'类型中不存在.
我试图将playOrPause属性声明为一种字符串,但它不起作用.我在这里错过了什么才能让它发挥作用?