gpt4 book ai didi

javascript - react + typescript : Property * is missing in type *

转载 作者:搜寻专家 更新时间:2023-10-30 20:58:12 25 4
gpt4 key购买 nike

我正在尝试遍历 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>;
}
}

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

此外,我正在使用 DefinitelyTyped 中的类型定义.

最佳答案

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

您可能混淆了 MyPropsCommentList(不包含 .author)和 MyProps注释(不包含.data)。

如果您对这两个组件使用不同 Prop 接口(interface)会更好。

关于javascript - react + typescript : Property * is missing in type *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32430946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com