[英]Why does the react 18.2 tutorial throws TS2339 Property 'X' does not exist on type 'Readonly<{}>'
I am currently following the react tutorial but at one point I am getting the TS2339 Property 'value' does not exist on type 'Readonly<{}>' error我目前正在关注反应教程,但有一次我收到TS2339 Property 'value' does not exist on type 'Readonly<{}>'错误
class Board extends React.Component {
renderSquare(i) {
return <Square value={i} />;
}
}
class Square extends React.Component {
render() {
return (
<button className="square">
{this.props.value}
</button>
);
}
}
Why do I get this error, but the tutorial is working fine?为什么我会收到此错误,但教程工作正常? I know how to fix it by eg adding the properties to the class or by using an interface.我知道如何通过将属性添加到 class 或使用接口来修复它。
class Square extends React.Component<{value: string}, {}> {
render() {
return (
<button className="square">
{this.props.value}
</button>
);
}
}
class Square extends React.Component<ISquare> {
render() {
return (
<button className="square">
{this.props.value}
</button>
);
}
}
interface ISquare {
value: string;
}
But why do I need to change something on my side?但是为什么我需要改变一些东西呢? As far as I see the tutorial is using the same React version.据我所知,本教程使用的是相同的 React 版本。
Why do I get this error, but the tutorial is working fine?为什么我会收到此错误,但教程工作正常?
Because that is a JavaScript tutorial, not a TypeScript tutorial, and there is no type validation in JavaScript.因为那是 JavaScript 教程,而不是 TypeScript 教程,并且 JavaScript 中没有类型验证。
Trying to do React.Component<{value: string}, {}>
in a JavaScript file would be a syntax error.尝试在 JavaScript 文件中执行React.Component<{value: string}, {}>
将是语法错误。
But why do I need to change something on my side?但是为什么我需要改变一些东西呢? As far as I see the tutorial is using the same React version.据我所知,本教程使用的是相同的 React 版本。
Because you're using TypeScript and not JavaScript.因为您使用的是 TypeScript 而不是 JavaScript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.