I'm using typescript 2.3.4 with React. I'm getting the error TS2339: error TS2339: Property 'name' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'. The error happens when I try to declare the property in a child component. How do I reference the property, properly in the child component?

由于某些原因,代码没有在脚本运行程序中执行.

感谢您的帮助.

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>

推荐答案

您忘记在ProfileData类定义中将name声明为React属性.像这样的方法应该有用:

class ProfileData extends React.Component<{name: string}, person> {

Typescript相关问答推荐

在TypeScript中,除了映射类型之外还使用的`in`二进制运算符?

在泛型类型中对子代执行递归时出错

打印脚本丢失函数的泛型上下文

具有泛型类方法的类方法修饰符

如何推断计算逻辑的类型

类型脚本返回的类型包含无意义的泛型类型名称

一个打字类型可以实现一个接口吗?

笛卡尔产品类型

有没有可能创建一种类型,强制在TypeScrip中返回`tyPeof`语句?

如何在Angular 服务中制作同步方法?

如何将类似数组的对象(字符串::匹配结果)转换为类型脚本中的数组?

将接口映射到交叉口类型

使用或属性编写无限嵌套的接口

在Google授权后由客户端接收令牌

无法缩小深度嵌套对象props 的范围

如何使函数参数数组不相交?

在类型脚本中创建显式不安全的私有类成员访问函数

换行TypeScript';s具有泛型类型的推断数据类型

Typescript 是否可以区分泛型参数 void?

错误:仅允许在‘使用服务器’文件中导出异步函数