Interesting TypeScript issue

I recently ran into the issue as following:

const actions = {
call: (num: number) => {
// todo
},
speak: (text: string) => {
// todo
},
}

// I’m going to create a function which can pass the action
// and its corresponding ar…


This content originally appeared on DEV Community and was authored by tmhao2005

I recently ran into the issue as following:

const actions = {
  call: (num: number) => {
    // todo
  },
  speak: (text: string) => {
    // todo
  },
}

// I'm going to create a function which can pass the action 
// and its corresponding argument as following:
doThings('call', 911);
doThings('speak', 'hello');

// so here the simple implemenatation
type Actions = typeof actions;
function doThings<T extends keyof Actions>(p0: T, ...[p1]: Parameters<Actions[T]>) {
  const action = actions[p0];
  // error!
  // Argument of type 'string | number' is not assignable to parameter of type 'never'.
  action(p1);
}

As we can see, it has a typing issue as the comment. I quickly searched for solution, then I ended up following up this issue which was open long ago https://github.com/microsoft/TypeScript/issues/30581.

Looks like we haven't yet had a good solution for the issue though. I eventually came up with a solution after reading a few solutions/hackarounds from comments as following:

// create arguments map
type ArgsMap = {
  call: [num: number, message: string];
  speak: [message: string];
};

// this is a key where we have to define the type for the object
const actions: {[K in keyof ArgsMap]: (...p: ArgsMap[K]) => void} = {
  call: (num, message) => {
    // todo
  },
  speak: (text) => {
    // todo
  },
}
// eventually the function signature 
function getAcion<K extends keyof ArgsMap>(name: K, ...p: ArgsMap[K]) {
  const action = actions[name]
  // works!
  action(...p)
}


This content originally appeared on DEV Community and was authored by tmhao2005


Print Share Comment Cite Upload Translate Updates
APA

tmhao2005 | Sciencx (2024-08-16T17:24:09+00:00) Interesting TypeScript issue. Retrieved from https://www.scien.cx/2024/08/16/interesting-typescript-issue/

MLA
" » Interesting TypeScript issue." tmhao2005 | Sciencx - Friday August 16, 2024, https://www.scien.cx/2024/08/16/interesting-typescript-issue/
HARVARD
tmhao2005 | Sciencx Friday August 16, 2024 » Interesting TypeScript issue., viewed ,<https://www.scien.cx/2024/08/16/interesting-typescript-issue/>
VANCOUVER
tmhao2005 | Sciencx - » Interesting TypeScript issue. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/16/interesting-typescript-issue/
CHICAGO
" » Interesting TypeScript issue." tmhao2005 | Sciencx - Accessed . https://www.scien.cx/2024/08/16/interesting-typescript-issue/
IEEE
" » Interesting TypeScript issue." tmhao2005 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/16/interesting-typescript-issue/. [Accessed: ]
rf:citation
» Interesting TypeScript issue | tmhao2005 | Sciencx | https://www.scien.cx/2024/08/16/interesting-typescript-issue/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.