简体   繁体   English

为什么 react 18.2 教程会抛出 TS2339 Property 'X' does not exist on type 'Readonly<{}>'

[英]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.

相关问题 错误TS2339:类型“ Readonly &lt;{}&gt;”上不存在属性“ items” - error TS2339: Property 'items' does not exist on type 'Readonly<{}>' 错误TS2339:类型“ Readonly &lt;{}&gt;”上不存在属性“ items”。 usnig在SPX中做出反应 - error TS2339: Property 'items' does not exist on type 'Readonly<{}>'. usnig react in SPX TS2339:类型“{}”上不存在属性X - TS2339: Property X does not exist on type '{}' TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript 类型“X[]”上不存在属性“customProperty”。 打字稿反应。 TS2339 - Property 'customProperty' does not exist on type 'X[]'. Typescript React. TS2339 'Readonly<{}> & Readonly<{ children?: ReactNode; 类型上不存在属性 }>'。 TS2339 - Property does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'. TS2339 错误 TS2339 - 类型 'Readonly&lt;{}&gt;' 上不存在属性 'x' - Error TS2339- Property 'x' does not exist on type 'Readonly<{}>' TS2339(TS)属性“状态”在类型上不存在 - TS2339 (TS) Property 'state' does not exist on type ts2339) 类型“{}”上不存在属性电话 - ts2339) Property phone does not exist on type '{}' 类型“{}”上不存在属性“xy”。 TS2339 - Property 'xy' does not exist on type '{}'. TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM