[英]Typesafe react decorator not recognizing the ReturnType of the passed function
I'm trying to extend the redux connect so it can be used with particular reducer/state as a decorator, it's probably not necessary because redux connect can be used as a decorator, but I am curious why I can't make it work the way I want. 我正在尝试扩展redux连接,以便它可以与特定的reducer / state一起用作装饰器,这可能不是必需的,因为redux connect可以用作装饰器,但是我很好奇为什么我无法使其工作我想要的方式
This is the HOC that I am using as a decorator: 这是我用作装饰器的HOC:
interface InjectedProps { userId: string; }
type ComponentType<P> = React.ComponentClass<P> | React.StatelessComponent<P>;
type StateToProps = (setting: ApplicationState) => InjectedProps;
export function withUserSetting(
stateToProps?: StateToProps
): <P extends InjectedProps>(WrappedComponent: ComponentType<P>) => void {
return <P extends InjectedProps>(Comp: ComponentType<P>) => {
class NewComponent extends (Component || PureComponent)<P> {
render() {
return <Comp {...this.props} />;
}
}
return connect(stateToProps)(NewComponent as any);
};
}
It works fine and it will alert me if the Props is missing, that's because it expecting return type of 'InjectedProps': 它工作正常,如果缺少道具,它将提醒我,这是因为它期望返回类型为'InjectedProps':
however, I would like to modify hoc so it can alert me based on the return type of the 'stateToProps': 但是,我想修改hoc,以便它可以根据“ stateToProps”的返回类型来提醒我:
type AnyFunction = (...args: any[]) => any;
type ComponentType<P> = React.ComponentClass<P> | React.StatelessComponent<P>;
type StateToProps = (setting: ApplicationState) => { [key: string]: any };
export function withUserSetting<T extends AnyFunction>(
stateToProps?: StateToProps
): <P extends ReturnType<T>>(WrappedComponent: ComponentType<P>) => void {
return <P extends ReturnType<T>>(Comp: ComponentType<P>) => {
class NewComponent extends (Component || PureComponent)<P> {
render() {
return <Comp {...this.props} />;
}
}
return connect(stateToProps)(NewComponent as any);
};
}
as you can see the 'InjectedProps' is not required anymore so it can have any prop name, and I am assuming because of the 'ReturnType' decorator should recognize props automatically and alert me if it's not declared for the component, but it doesn't have any effect: 如您所见,'InjectedProps'不再需要,因此它可以具有任何道具名称,并且我假设由于'ReturnType'装饰器应自动识别道具,并在没有为组件声明时提醒我,但它没有。没有任何效果:
decorator works fine, however, I don't have a type safety I am looking for! 装饰器工作正常,但是,我没有所需的类型安全性! any idea why it's not working?
知道为什么它不起作用吗?
After simplifying the code, and understand it better, I have managed to make it work, this is the working version: 在简化代码并更好地理解它之后,我设法使其正常运行,这是有效的版本:
type ComponentType<P> = React.ComponentClass<P> | React.StatelessComponent<P>;
type MapStateToProps<T> = (setting: ApplicationState) => T;
export function withUserSetting<T>(mapStateToProps?: MapStateToProps<T>):
<P extends T>(WrappedComponent: ComponentType<P>) => void {
return <P extends T>(WrappedComponent: ComponentType<P>) => {
return connect(mapStateToProps)(WrappedComponent as any);
};
}
Now it can be used like this: 现在可以像这样使用:
@withUserSetting((state) => ({ userId: state.userSetting.userId }))
Or like this: 或像这样:
@withUserSetting<UserProps>((state) => ({ userId: state.userSetting.userId }))
In both ways, it will raise an error if a property is missing in the declaration. 无论哪种方式,如果声明中缺少属性,都会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.