Reactjs 是否可以根据父组件推断组件属性的类型?

Reactjs 是否可以根据父组件推断组件属性的类型?,reactjs,typescript,redux,react-redux,Reactjs,Typescript,Redux,React Redux,我有这个容器 export interface State { email: string } const mapStateToProps = (state: State) => ({ email: state.email, }) const mapDispatchToProps = (dispatch: Dispatch<Action>) => ({ onChange: (name: string, value: string) => dispat

我有这个容器

export interface State {
  email: string
}

const mapStateToProps = (state: State) => ({
  email: state.email,
})

const mapDispatchToProps = (dispatch: Dispatch<Action>) => ({
  onChange: (name: string, value: string) => dispatch(/*...*/),
})

export default connect(mapStateToProps, mapDispatchToProps)(Login)
导出接口状态{
电子邮件:string
}
常量mapStateToProps=(状态:state)=>({
电子邮件:state.email,
})
const mapDispatchToProps=(调度:调度)=>({
onChange:(名称:string,值:string)=>dispatch(/*…*/),
})
导出默认连接(mapStateToProps、mapDispatchToProps)(登录)
这个组件呢

export interface LoginProps {
  email: string
  onChange: (name: string, value: string) => void
}

const Login = (props: LoginProps) => (
  <p>Render something here</p>
)
导出接口登录操作{
电子邮件:string
onChange:(名称:string,值:string)=>void
}
常量登录=(道具:LoginProps)=>(
在这里渲染一些东西

)

有没有一种方法可以根据
容器定义推断登录属性的类型,这样我就不必手动定义
LoginProps

对于
connect
,有很好的类型定义,因此可以推断传递给包装组件的道具的类型。如果您像这样编写容器/组件

export interface State {
  email: string
}

const stateToProps = (state: State) => ({
    email: state.email
  })

const dispatchToProps = (dispatch: Dispatch<Action>) => ({
    onChange: (name: string, value: string) => dispatch(/*...*/)
})

const BlahBlah = connect(stateToProps, dispatchToProps)(({ email, onChange }) => (
    <div />
));
无法推断
道具的类型。这是因为除了将
Login
传递到
connect
之外,您还可以直接调用它。例如,我可以在组件中编写:

render() {
    return (
        <Login unrelated="a string" typescript="cool" />
    );
}
render(){
返回(
);
}

如果登录组件上没有注释,编译器就无法知道
Login
的哪个调用(连接的调用或我的直接呈现)提供了正确的道具。如果没有注释,那么编译器只能将道具键入为
any
,因此我们就失去了类型安全性。

您能提供一个示例,说明您希望代码的外观吗?理想情况下,我希望能够不指定任何内容,只是推断出它非常重要,这完全有意义
render() {
    return (
        <Login unrelated="a string" typescript="cool" />
    );
}