我使用的是typescript 2.3。4与反应。我收到错误TS2339:错误TS2339:类型“Readonly”上不存在属性“name”
由于某些原因,代码没有在脚本运行程序中执行。
感谢您的帮助。
export interface person {
name: string;
age: number;
}
interface State {
personArray: person[];
}
interface Props {
}
class ProfileData extends React.Component<{}, person> {
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>{this.props.name}</div>
</section>
</section>
)
}
}
export class Profile extends React.Component<Props, State> {
public state: State;
public props: Props;
constructor(props: Props){
super(props);
this.state = {
personArray: [
{
name: 'bazaaa',
age: 42
},
{
name: 'louis',
age: 24
}
]
};
}
public render() {
let profile1 = this.state.personArray[0];
return (
<ProfileData
name={profile1.name}
/>
)
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
您忘记将name
声明为ProfileData
类定义中的React属性。这样的东西应该工作:
class ProfileData extends React.Component<{name: string}, person> {