我正在使用具有有趣的类型声明(简化)的库:
class Emitter {
public on<EventName extends 'streamCreated' | 'streamDestroyed'> (eventName: EventName, callback: (event: {
streamCreated: {id: number};
streamDestroyed: {reason: string};
}[EventName]) => void): void;
}
并且我正在尝试在提供的回调中键入事件:
const callback = (event: StreamDestroyedEvent) => console.log(event.reason);
emitter.on('streamDestroyed', callback);
但是“ StreamDestroyedEvent”不存在。它不是由库提供的,而是仅存在于该匿名事件映射中,因此我改为尝试推断它:
type InferCallback<T, E> = T extends (eventName: E, event: infer R) => void ? R : never;
type InferCallbackEvent<T, E> = InferCallback<T, E> extends (event: infer P) => void ? P : never;
type StreamDestroyedEvent = InferCallbackEvent<Emitter['on'], 'streamDestroyed'>;
但是,不是给我类型{reason: string}
,而是联合类型{reason: string;} | {id: number;}
。如何获得正确的类型,或者这与我将要获得的接近?
您基本上想为EventName
类型参数“插入”一种类型。当calling泛型函数支持TypeScript时,它在类型系统本身<< representing >>这样的类型中做得不好。要完全这样做,将需要支持higher rank types,而这实际上并不是该语言的一部分。而且emitter.on
函数本身并不能真正让您“部分地”调用它,因此您要使用第一个参数插入EventName
,然后让编译器告诉you第二个参数的类型是。在TypeScript 3.4出现之前,我可能会说不可能从编译器中获取此信息。我会尝试类似
emitter.on("streamDestroyed", x => {
type StreamDestroyedEvent = typeof x; // can't get it out of the scope though!
})
然后无法获得StreamDestroyedEvent
类型以转义该函数。
TypeScript 3.4引入了改进的支持higher order type inference from generic functions。您仍然不能仅表示类型系统中正在执行的操作,但是现在我们可以定义一个函数,该函数为我们提供可以“部分”调用并保留所需类型信息的功能。
这里是currying函数,它采用多参数函数并返回一个参数的新函数,该函数返回其余参数的另一个函数:
const curry = <T, U extends any[], R>(
cb: (t: T, ...args: U) => R
) => (t: T) => (...args: U) => cb(t, ...args);
如果您在,(在TS3.4 +中),您会得到一个函数,该函数带有一个回调,该回调带有一个curry
上调用emitter.on
,然后用"streamDestroyed"
调用that
StreamDestroyedEvent
,您可以现在可以捕获:const streamDestroyedFunc = curry(emitter.on.bind(emitter))("streamDestroyed")
type StreamDestroyedEvent = Parameters<Parameters<typeof streamDestroyedFunc>[0]>[0];
// type StreamDestroyedEvent = { reason: string; }
请注意,上面的实际运行时行为不是重点;我试图确保它实际上会做一些合理的事情,但是只要您在运行时没有遇到运行时错误,您也可以使用类型断言来向编译器说谎,以了解发生了什么。
const fakeCurry: typeof curry = () => () => null!; const fakeOn: Emitter["on"] = null!; const fakeFunc = fakeCurry(fakeOn)("streamDestroyed"); type SDE2 = Parameters<Parameters<typeof fakeFunc>[0]>[0]; // same thing
好的,希望能有所帮助;祝你好运!