如何理解<Props, {}>这个定义?

#1
/// <reference path="react.d.ts" />

interface Props {
    foo: string;
}

class MyComponent extends React.Component<Props, {}> {
    render() {
    return <span>{this.props.foo}</span>
    }
}

<MyComponent foo="bar" />; // 正确
<MyComponent foo={0} />; // 错误

是类型检查,这是一种语法还是其他的? 我可以在construct中去检查么?

#2

这是typescript,<Props,{}>里的Props指的是这个组件接收的props的类型,{}表示的是这个组件的state的类型

#3

主要是不是很理解<> 这种写法? 我去看了typescript 这是类型断言么? 但是类 可以这样写?我没看到具体的资料 没说这是一种语法 之类的 不知道为啥这样写 也就是

#4

typescript的泛型

1 Like
#5

看到了 谢谢!