DEV Community

tmhao2005
tmhao2005

Posted on

1

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 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);
}
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay