React + Typescript:属性 * 类型中缺少 *
React + Typescript: Property * is missing in type *
我正在尝试完成 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 中的类型定义。
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'.
您可能混淆了 MyProps
与 CommentList
(不包含 .author
)和 MyProps
与 Comment
(不包含 .data
]).
如果你对两个组件使用不同的 Prop 接口会更好。
我正在尝试完成 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 中的类型定义。
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'.
您可能混淆了 MyProps
与 CommentList
(不包含 .author
)和 MyProps
与 Comment
(不包含 .data
]).
如果你对两个组件使用不同的 Prop 接口会更好。