Typescript 如何键入参数化元组?

Typescript 如何键入参数化元组?,typescript,Typescript,我有一个元组,其中类型相互关联。在我的例子中,它是一个提取器函数,它提取一个值,然后作为另一个函数的输入 export interface API = { saveModel : (model:Model) => Promise<boolean>, getModel : (modelID:string) => Promise<Model>, } const api: API = { ... } // this is the tupl

我有一个元组,其中类型相互关联。在我的例子中,它是一个提取器函数,它提取一个值,然后作为另一个函数的输入

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)
}
从概念上讲,我想要的是这样的东西,但这并没有编译:

const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
我可以用其他的方法来解决这个问题,这让我觉得元组可能有些东西我还没有理解

从概念上讲,我想要的是这样的东西,但这并没有编译:

const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
是的,那是一口。它可以分解为:

// Use universal quantification for the base type
type WirePlanEntry<T> = [string, (msg: any) => T, (t: T) => Promise<any>]
// A WirePlanEntryConsumer<R> takes WirePlanEntry<T> for any T, and outputs R
type WirePlanEntryConsumer<R> = <T>(plan: WirePlanEntry<T>) => R
// This consumer consumer consumes a consumer by giving it a `WirePlanEntry<T>`
// The type of an `EWirePlanEntry` doesn't give away what that `T` is, so now we have
// a `WirePlanEntry` of some unknown type `T` being passed to a consumer.
// This is the essence of existential quantification.
type EWirePlanEntry = <R>(consumer: WirePlanEntryConsumer<R>) => R
// this is an application of the fact that the statement
// "there exists a T for which the statement P(T) is true"
// implies that
// "not for every T is the statement P(T) false"

// Convert one way
function existentialize<T>(e: WirePlanEntry<T>): EWirePlanEntry {
  return <R>(consumer: WirePlanEntryConsumer<R>) => consumer(e)
}

// Convert the other way
function lift<R>(consumer: WirePlanEntryConsumer<R>): (e: EWirePlanEntry) => R {
  return (plan: EWirePlanEntry) => plan(consumer)
}
但如果你有像这样的消费者

function consume<T>(plan: WirePlanEntry<T>): R // R is not a function of T
不过,现在你可以有制片人了。最简单的生产者已经写出来了:
存在主义

以下是代码的其余部分:

type WirePlan = EWirePlanEntry[]
const wirePlan: WirePlan = [
  existentialize(['saveModel', (msg:any) => <Model>msg.model   , api.saveModel]),
  existentialize(['getModel' , (msg:any) => <string>msg.modelID, api.getModel ]),
]

const handleMessage = (msg: any) => {
  let entry = wirePlan.find(lift((w) => w[0] === msg.name))
  if(entry) {
    entry(handler => {
      const extractedValue = handler[1](msg)
      return handler[2](extractedValue)
    })
  }
}
类型WirePlan=Ewireplantery[]
常数线计划:线计划=[
存在化(['saveModel',(msg:any)=>msg.model,api.saveModel]),
存在化(['getModel',(msg:any)=>msg.modelID,api.getModel]),
]
const handleMessage=(消息:any)=>{
let entry=wirePlan.find(lift((w)=>w[0]==msg.name))
如果(条目){
条目(处理程序=>{
const extractedValue=handler[1](消息)
返回处理程序[2](extractedValue)
})
}
}

发布虚构的语法没有多大帮助-您能否展示一下如何使用这个
a
值,即您希望合法的示例和您希望非法的示例?好的。我用一个具体的例子来充实它。我不确定我是否理解这个问题,但这可能有用吗<代码>键入getSet=[()=>T,(val:T)=>T]@ThomasDevries这可能是一种方法。
type WirePlan = EWirePlanEntry[]
const wirePlan: WirePlan = [
  existentialize(['saveModel', (msg:any) => <Model>msg.model   , api.saveModel]),
  existentialize(['getModel' , (msg:any) => <string>msg.modelID, api.getModel ]),
]

const handleMessage = (msg: any) => {
  let entry = wirePlan.find(lift((w) => w[0] === msg.name))
  if(entry) {
    entry(handler => {
      const extractedValue = handler[1](msg)
      return handler[2](extractedValue)
    })
  }
}