In my scenario, I have a tuple with interrelated types. Specifically, it involves an extractor function that retrieves a value, which is then used as input for another function.
What I envision conceptually looks like this code snippet, although it does not compile:
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
The purpose of this setup is to handle incoming RPC messages of type any
, in conjunction with an API that uses specific argument types. Essentially, I aim to create a "wiring plan" consisting of pairs of extractor functions and corresponding API functions.
export interface API = {
saveModel : (model:Model) => Promise<boolean>,
getModel : (modelID:string) => Promise<Model>,
}
const api: API = { ... }
// The desired tuple type definition specifies the relationship between the second and third elements.
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 find alternative solutions to the problem at hand, but it occurred to me that there might be nuances about tuples that I haven't fully grasped.