最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How to define types definition for react custom hook with generics? - Stack Overflow

programmeradmin3浏览0评论

Let's consider these type definition for a react custom hook that has as params a mutation trigger and a mutation result of an api call of redux toolkit:

type UseCallToActionModalRendererParams<T extends (...args: any) => any> = {
    mutationTrigger: () => ReturnType<ReturnType<T>[0]>;
    mutationResult: ReturnType<T>[1];
};

type UseCallToActionModalRendererResult = {
    title: string;
};

type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (
    params: UseCallToActionModalRendererParams<T>,
) => UseCallToActionModalRendererResult;

This doesn't work:

export const useCallToActionModalRenderer: UseCallToActionModalRendererHook = ({
    mutationTrigger,
    mutationResult,
}) => {

with the following error

Generic type 'UseCallToActionModalRendererHook' requires 1 type argument(s).ts(2314)
type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (params: UseCallToActionModalRendererParams<T>) => UseCallToActionModalRendererResult

And this works:

export const useCallToActionModalRenderer = <T extends (...args: any) => any>({
    mutationTrigger,
    mutationResult,
}: UseCallToActionModalRendererParams<T>): UseCallToActionModalRendererResult => {

The custom hook is called with T set, for example, as this type , where useCancelDataMutation is a mutation of redux-toolkit.

It seems the same approach, with a definition or for full hook, or for input and outputs. But why first approach is failing?

Let's consider these type definition for a react custom hook that has as params a mutation trigger and a mutation result of an api call of redux toolkit:

type UseCallToActionModalRendererParams<T extends (...args: any) => any> = {
    mutationTrigger: () => ReturnType<ReturnType<T>[0]>;
    mutationResult: ReturnType<T>[1];
};

type UseCallToActionModalRendererResult = {
    title: string;
};

type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (
    params: UseCallToActionModalRendererParams<T>,
) => UseCallToActionModalRendererResult;

This doesn't work:

export const useCallToActionModalRenderer: UseCallToActionModalRendererHook = ({
    mutationTrigger,
    mutationResult,
}) => {

with the following error

Generic type 'UseCallToActionModalRendererHook' requires 1 type argument(s).ts(2314)
type UseCallToActionModalRendererHook<T extends (...args: any) => any> = (params: UseCallToActionModalRendererParams<T>) => UseCallToActionModalRendererResult

And this works:

export const useCallToActionModalRenderer = <T extends (...args: any) => any>({
    mutationTrigger,
    mutationResult,
}: UseCallToActionModalRendererParams<T>): UseCallToActionModalRendererResult => {

The custom hook is called with T set, for example, as this type , where useCancelDataMutation is a mutation of redux-toolkit.

It seems the same approach, with a definition or for full hook, or for input and outputs. But why first approach is failing?

Share Improve this question edited Mar 18 at 15:02 Drew Reese 204k18 gold badges245 silver badges273 bronze badges asked Mar 18 at 9:32 axelaxel 4,1375 gold badges51 silver badges81 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

But why first approach is failing?

Because you have to supply a type. You aren't supplying a type there.

From that type, the types of mutationTrigger and mutationResult within the body of useCallToActionModalRenderer are constrained, but there can be multiple distinct uses with different specific types.

发布评论

评论列表(0)

  1. 暂无评论