简体   繁体   English

Typesafe React装饰器无法识别所传递函数的ReturnType

[英]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.

相关问题 React组件无法识别.filter为函数 - React component not recognizing .filter as a function React:props 中传递的函数未定义 - React: function passed in props is undefined 从与 React 相关的 MUI 样式组件中传递的道具中获取警告,但无法识别它 - Getting warning from props passed in MUI styled components related to React not recognizing it React.js - 传递函数“不是函数”错误 - React.js - Passed function “not a function” Error 函数返回函数的通用TypeScript类型,该函数将ReturnType替换为返回函数的ReturnType - Generic TypeScript Type for functions returning functions that replaces the ReturnType with the ReturnType of the returned function 由于无法识别内置功能,无法对反应组件进行统一测试 - unite testing of react component fails for not recognizing a built in function 在React / Redux项目中,如何使Typescript知道redux的connect装饰器传递的属性? - In a React/Redux project how to make Typescript aware of property passed by redux's connect decorator? 箭头 function 在 React-Native 中作为道具传递在传递给它的组件中未定义 - Arrow function passed as prop in React-Native is undefined in component it is passed to 在React中作为参数传递的参数化函数的奇怪行为 - Strange behavior of parametrised function passed as a prop in React 如何使用传递给 React 函数的参数设置状态 - How to Set State with arguments passed to a function in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM