'Can typescript infer nested values from a function argument?
I want to switch which args are passed, based on on the target. It seems like typescript can't infer what is passed in the function argument. Is there a way to help it figure out / write it differently so that it can infer it ?
Currently it is making a union out of all the passed arguments, but I would like it to pick out the correct argument.
const allowedTargets = {
A: ({hiA}: {hiA: string})=>{},
B: ({hiB}: {hiB: number})=>{},
}
type AllowedT = "A" | "B"
type ArgsOf<T extends AllowedT> = Parameters<typeof allowedTargets[T]>[0]
type Target<T extends AllowedT> = {target: T, args: ArgsOf<T> }
type TargetContainer<T extends AllowedT> = Record<string, Target<T>>
type MyFn = <T extends AllowedT>(arg0: TargetContainer<T>) => void
const myFn: MyFn = ()=>{}
// args are a union of all possible args
myFn({"hiA": {target: "A", args: {hiA: ""}}, "hiB": {target: "B", args: {hiA: ""}}})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
