简体   繁体   English

如何使用Typescript在外部声明的类型创建泛型函数?

[英]How to create a generic function with type declared externally in Typescript?

I have the following function (it is a React hook): 我有以下功能(它是一个React钩子):

type HandleEditFn = (name: string) => (data: any) => React.MouseEventHandler;

type useFormToggleHook = (params: {
    forms: {
        [propName: string]: React.ReactType;
    };
    formPropsFn?: (data: any) => object;
    refetch?: any;
}) => [...];

export const useFormToggle: useFormToggleHook = ({
    ...
}) {

    const handleEdit : HandleEditFn = name => data => e => {
        ...
    }

    return [handleEdit];

}

Now, I want to make this function generic; 现在,我想使此函数通用; so that, components who call this hook can do the following: 这样,调用此挂钩的组件可以执行以下操作:

const [handleEdit] = useFormToggle<UserData>(...);

So, I added the generic syntax to types: 因此,我将通用语法添加到类型中:

type HandleEditFn<T> = (name: string) => (data: T) => React.MouseEventHandler;

type useFormToggleHook<T> = (params: {
    forms: {
        [propName: string]: React.ReactType;
    };
    formPropsFn?: (data: T) => object;
    refetch?: any;
}) => [HandleEditFn<T>, ...];

// Type here doesn't work
export const useFormToggle: useFormToggleHook<T> = ({
    ...
}) {

    // Type is required here!
    const handleEdit : HandleEditFn<T> = name => data => e => {
        ...
    }

    return [handleEdit, ...];

}

When I add useFormToggleHook<T> for useFormToggle function, I get an error that T is not a valid type. 当我为useFormToggle函数添加useFormToggleHook<T> ,出现错误消息T不是有效的类型。


I have read the following Stackoverflow post: TypeScript Type of generic function but this did not answer my question. 我已经阅读了以下Stackoverflow帖子: TypeScript泛型函数的类型,但这不能回答我的问题。 I know that I can do the following: 我知道我可以执行以下操作:

export const useFormToggle : <T>(params: {
    forms: {
        [propName: string]: React.ReactType;
    },
    formPropsFn?: (data: any) => object;
    refetch?: any;
}) = ({ ... }) => ...

However, I want to declare the type for the function separately. 但是,我想分别声明函数的类型。 Is this possible with Typescript? Typescript可以吗?

You have to declare your generic type somewhere to make it work. 您必须在某个地方声明泛型类型才能使其正常工作。

type HandleEditFn makes HandleEditFn generic and whatever is passed to it, will be T. 类型HandleEditFn使HandleEditFn通用,并且传递给它的任何东西都将为T。

Your problem is, that you do not pass an actual type to HandleEditFn with: 您的问题是,您没有通过以下方式将实际类型传递给HandleEditFn:

 const handleEdit : HandleEditFn<T> = name => data => e => {}

because T is not a type which is declare somewhere. 因为T不是在某处声明的类型。

To make it work and to declare T in HandleEditFn you have to call it like this: 为了使其工作并在HandleEditFn声明T,您必须这样调用它:

const handleEdit : HandleEditFn<string> = name => data => e => {}

This will make data string for example but you can pass any type or interface to define T and use it as data. 例如,这将形成数据字符串,但是您可以传递任何类型或接口来定义T并将其用作数据。

The same applies to useFormToggleHook: 同样适用于useFormToggleHook:

export const useFormToggle: useFormToggleHook<string> = ()

Hope this helps. 希望这可以帮助。 Happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM