Skip to content

Instantly share code, notes, and snippets.

@siassaj
Last active January 1, 2019 05:03
Show Gist options
  • Select an option

  • Save siassaj/5d627b44eb8b48728d1b0f705df3c1b5 to your computer and use it in GitHub Desktop.

Select an option

Save siassaj/5d627b44eb8b48728d1b0f705df3c1b5 to your computer and use it in GitHub Desktop.
interface Action<K extends keyof typeof ops> {
action: K;
data: (typeof ops[K]) extends (arg: infer T) => any ? T : undefined
}
const ops = {
setFire: (data: { place: string }) => data,
extinguish: (data: { extinguisherType: string }) => data
}
// this works fine, but is a little verbose
const action: Action<"setFire"> = {
action: "setFire",
data: { place: "over there" }
}
// this doesn't work, is there a way to make it work by changing the type signature of Action ?
const action: Action = {
action: "setFire",
data: { place: "over there" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment