If I have the following union which wraps a function and its argument, how can I call it?
type Wrapper = {
fn: (a: string) => void
arg: string
} | {
fn: (a: number) => void
arg: number
}
let foo!: Wrapper
foo.fn(foo.arg) // Error Type 'string' is not assignable to type 'never'.
I can't figure out how to call it. Everything that I've tried essentially boils down to a cast, (e.g. casting to (a:any) => void) which I can do if I have to, but I feel that I shouldn't have to do that.
Can this function be called without casting?
Edit: To clarify, I'm asking if there exist solutions which don't involve changing definition of Wrapper.
type F = (x: A) => Tandtype G = (x: B) => U, the uniontype H = F | Gis(x: A & B) => T | U. To call a function of typeH, we would need supply an argument that is a subtype of bothAandB.fnandarg.argto be a discriminant. You would have to introduce a discriminant i.e. Playground Link