【问题标题】:React + Typescript: Property * is missing in type *React + Typescript:类型 * 中缺少属性 *
【发布时间】:2015-12-02 13:17:12
【问题描述】:

我正在尝试通过React Tutorial,但出现了一个我不明白的错误。

错误信息是:

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'.

这里是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'));
});

还有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>;
    }
}

我记得读过接口仅用于类型检查并且不存在于转译代码中。但是,我仍然不完全理解为什么会出现这些错误。

另外,我正在使用来自DefinitelyTyped 的类型定义。

【问题讨论】:

    标签: javascript reactjs typescript react-jsx


    【解决方案1】:

    comment.tsx(30,5):错误 TS2324:“MyProps”类型中缺少属性“数据”。 comment.tsx(31,5):错误 TS2324:“MyProps”类型中缺少属性“数据”。 main.tsx(20,17):错误 TS2324:“MyProps”类型中缺少属性“作者”。 main.tsx(27,18):错误 TS2324:“MyProps”类型中缺少属性“作者”。

    您可能会将MyPropsCommentList(不包含.author)和MyPropsComment(不包含.data)混淆。

    如果你对两个组件使用不同的 Prop 接口会更好。

    【讨论】:

    • 谢谢。创建单独的接口解决了这个问题。查看转译的 JS 也有帮助,基本上 &lt;Comment author="Pete Hunt"&gt;Some comment&lt;/Comment&gt; 变成了 React.createElement(Comment, {"author": "Pete Hunt"}, "Some comment")。所以 JavaScript 中的 prop 就是 {"author": "Pete Hunt"}。所以 TypeScript 告诉我我从未在 Comment 上使用过 data 属性?我对接口在 TypeScript 中的工作方式仍然有些模糊。基本上为什么会有两个MyProps
    • 两个独立的接口,用于两个独立的合同,用于两个独立的组件
    • 谢谢。我猜contract 是关键词。 React.Component&lt;P, S&gt; 的第一个参数 (P) 定义了属性的形状,所以我需要确保我使用的属性与之匹配。
    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 2017-11-10
    • 2021-06-24
    • 1970-01-01
    • 2020-05-30
    • 2020-08-24
    • 2016-08-15
    • 2021-01-05
    相关资源
    最近更新 更多