React + Typescript: property * is absent in type *

I am trying to get through the React Tutorial , but an error has occurred that I do not understand.

Error messages:

comment.tsx(30,5): error TS2324: Property 'data' is missing in type 'MyProps'.
comment.tsx(31,5): error TS2324: Property 'data' is missing in type 'MyProps'.
main.tsx(20,17): error TS2324: Property 'author' is missing in type 'MyProps'.
main.tsx(27,18): error TS2324: Property 'author' is missing in type 'MyProps'.

Here main.tsx:

import * as React from 'react';
import 'jquery';
import { CommentList, CommentForm, MyProps } from './comment';

var data = [
  {author: "Pete Hunt", text: "This is one comment"},
  {author: "Jordan Walke", text: "This is *another* comment"}
];

class CommentBox extends React.Component<MyProps, {}> {
    render() {
        return <div className="commentBox">
                <h1>Comments</h1>
                <CommentList data={this.props.data} />
                <CommentForm />
                </div>;
    }
}

$(() => {
    React.render(<CommentBox data={data} />, document.getElementById('content'));
});

And comment.tsx:

import * as React from 'react';

export interface MyProps extends React.Props<any> {
    author: string;
    data: Array<any>;
}

export class Comment extends React.Component<MyProps, {}> {
    render() {
        let rawMarkup = marked(this.props.children.toString(), {sanitize: true});
        return <div className="comment">
                 <h2 className="commentAuthor">
                   {this.props.author}
                 </h2>
                 <span dangerouslySetInnerHTML={{__html: rawMarkup}} />
               </div>;
    }
}

export class CommentList extends React.Component<MyProps, {}> {
    render() {
        return <div className="commentList">
                <Comment author="Pete Hunt">Some comment</Comment>
                <Comment author="Jordan Walke">Another *comment*</Comment>
                </div>;
    }
}

export class CommentForm extends React.Component<{}, {}> {
    render() {
        return <div className="commentForm">
               A CommentForm
               </div>;
    }
}

I remember reading that the interfaces are intended only for type checking and do not exist in the transmitted code. However, I still do not quite understand why I am getting these errors.

In addition, I use type definitions from DefinitelyTyped .

+4
source share
1 answer

comment.tsx(30,5): TS2324: "MyProps" "". comment.tsx(31,5): TS2324: "MyProps" "". main.tsx(20,17): TS2324: 'author' 'MyProps'. main.tsx(27,18): TS2324: 'author' 'MyProps'.

, MyProps CommentList ( .author) MyProps Comment ( .data).

, Prop .

+4

Source: https://habr.com/ru/post/1606196/


All Articles