How to create an interface or generic type for an object where all its properties are functions with different arguments in Typescript?

Asked

Viewed 27 times

-2

Let’s say I have these objects:

const actionsPost = {
    likePost: (id: number) => dispatch({type: Actions.like, payload: {id}}),
    addPost: (title: string, body: string) => dispatch({type: Actions.add, payload: {title, body}}),
    updatePost: (id: number, title: string, body: string) => dispatch({type: Actions.add, payload: {id, title, body}}),
}

const actionsComment = {
    likeComment: (id: number) => dispatch({type: Actions.like, payload: {id}}),
    addComent: (postId: number, body: string) => dispatch({type: Actions.comment, payload: {postId, body}}),
}

How do I create an interface or generic type that matches both and any other I can create?

I tried this (assuming Dispatch always returns empty), but it didn’t work:

interface ActionsAggregator {
    [key: string]: <T extends any>(...args: T[]) => void
}

This one works, but it’s too wide and I’ve lost the code completion:

interface ActionsAggregator2 {
    [key: string]: Function
}
  • 1

    Hi! You are in Portuguese version of Stack Overflow site. If you Speak Portugese, Please Translate your Question, otherwise post your Question in Stack Overflow original site. Thanks for your visit.

  • @Felipegambini made

  • Do you want to make a type/interface that has any key and that any value is a function with any parameters? He doesn’t seem like a very helpful guy. There’s nothing more specific in there that I can make a better guy?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.