我有一个元组,其中的类型彼此相关。在我的例子中,它是一个提取器函数,它提取一个值,该值又用作另一个函数的输入。
从概念上讲,我正在寻找的是类似的东西,但这不能编译:
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
用例就是这样。我有一个类型为any
的传入RPC消息,以及一个具有众所周知的参数类型的API。我想构建一个“接线计划”,它接受两个参数,一个提取器函数和相应的API函数。
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)
}
我可以通过其他方式解决这个问题,它让我觉得可能有一些我不理解的元组。
答案 0 :(得分:2)
从概念上讲,我正在寻找的是类似的东西,但这不能编译:
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
实际上,就是你想要的相反。借助函数类型的直觉,a: <T>(t: T) => T
意味着您拥有适用于所有类型的函数。这是一个通用量词:a
的实现不知道T
是什么; a
的用户可以将T
设置为他们想要的任何内容。为元组执行此操作将是灾难性的,因为无论T
是什么,内部函数都需要输出T
的值,因此他们唯一可以做的就是错误输出/循环永久/底部以某种方式(他们必须返回never
)。
你想要existential quantification。 a: ∃T. [(v:any) => T, (t:T) => void]
表示a
与其关联的某些类型T
。 a
的实现知道它是什么,可以用它做任何事情,但a
的用户现在对它一无所知。实际上,与普遍量化相比,它颠倒了角色。 TypeScript doesn't have support for existential types(甚至不是像Java的通配符那样的超级基本形式),但can be simulated:
type WirePlanEntry = <R>(user: <T>(name: string, reader: (msg: any) => T, action: (t: T) => Promise<any>)) => R
type WirePlan = WirePlanEntry[]
是的,这是满口的。它可以分解为:
// 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)
}
消费EWirePlanEntry
看起来像
plan(<T>(eT: WirePlanEntry<T>) => ...)
// without types
plan(eT => ...)
但如果你只是有消费者喜欢
function consume<T>(plan: WirePlanEntry<T>): R // R is not a function of T
你会像
一样使用它们plan(consume) // Backwards!
lift(consume)(plan) // Forwards!
现在,你可以拥有生产者。最简单的这样的制作人已经写成:existentialize
。
以下是您的其余代码:
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)
})
}
}