I have a tuple where the types relate to each other. In my case it's an extractor function that extracts a value that is in turn used as input to another function.
Conceptually what I'm looking for is something like this, but this doesn't compile:
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
The use case is this. I have an incoming RPC message of type any, and an API with well known argument types. I want to build a "wiring plan" that takes two arguments, one extractor function and the corresponding API function.
export interface API = {
saveModel : (model:Model) => Promise<boolean>,
getModel : (modelID:string) => Promise<Model>,
}
const api: API = { ... }
// this is the tuple type where i'd like to define that
// there's a relation between the second and third member
// of the tuple.
type WirePlan = [[string, (msg:any) => T, (t:T) => Promise<any>]]
const wirePlan: WirePlan = [[
['saveModel', (msg:any) => <Model>msg.model , api.saveModel],
['getModel' , (msg:any) => <string>msg.modelID, api.getModel],
]
const handleMessage = (msg) => {
const handler = wirePlan.find((w) => w[0] === msg.name)
const extractedValue = handler[1](msg)
return handler[2](extractedValue)
}
I can work around the issue in other ways, it just struck me there may be something about tuples I haven't understood.