TypeScript:按值从对象中删除键
TypeScript: Remove keys from object by value
我有一个以键和函数作为值的对象。现在我想创建一个函数,您只能在其中分配相应键包含回调函数作为最后一个函数参数的键。
回调只能有一个参数并且return类型为void。
interface Events {
'valid0': (data: string, cb: () => void) => void, // valid
'valid1': (data: number, cb: (data: string) => void) => void, // valid
'invalid0': (data: string, cb: () => string) => void, // invalid return type of callback
'invalid1': (data: string, cb: (string: string, number: number) => void) => void, // invalid number of callback arguments
}
type EventsWithCallback<E> = ???
type testFunction<Events> = (EventName: EventsWithCallback<Events>) => void
我在定义此 EventsWithCallback 类型时遇到问题。通过以下我得到错误:Type 'T[P]' does not satisfy the constraint '(...args: any[]) => void'
。这在某种程度上是合乎逻辑的。我尝试将 T 键入为 Record<string, (...args: any) => void>
但随后我匹配了所有字符串。
type Last<T extends any[]> = T extends [...any, infer Last] ? Last : any;
type EventsWithCallback<T> = keyof { [P in keyof T as Last<Parameters<T[P]>> extends Function ? P : never]: T[P] };
另外 extends Function
匹配任何函数和任何类型。
感谢您提供的任何帮助。我希望这个问题是可以理解的。
这个有用吗? Playground Link
interface Events {
'valid0': (data: string, cb: () => void) => void, // valid
'valid1': (data: number, cb: (data: string) => void) => void, // valid
'invalid0': (data: string, cb: () => string) => void, // invalid return type of callback
'invalid1': (data: string, cb: (string: string, number: number) => void) => void, // invalid number of callback arguments
}
type Last<T extends any[]> = T extends [...any, infer Last] ? Last : any
type EventsWithCallback<T> = {
[P in keyof T]:
T[P] extends (...args: infer A) => void // Check if it is a function
? Last<A> extends (arg: any) => any // Check if the last parameter is a function with single parameter
? ReturnType<Last<A>> extends void // Check if it returns void
? P // return the parameter name
: never
: never
: never
}[keyof T]
const testFunction = (eventName: EventsWithCallback<Events>) => {
eventName = 'invalid0' // error
eventName = 'invalid1' // error
eventName = 'valid0' // no error
eventName = 'valid1' // no error
}
我有一个以键和函数作为值的对象。现在我想创建一个函数,您只能在其中分配相应键包含回调函数作为最后一个函数参数的键。
回调只能有一个参数并且return类型为void。
interface Events {
'valid0': (data: string, cb: () => void) => void, // valid
'valid1': (data: number, cb: (data: string) => void) => void, // valid
'invalid0': (data: string, cb: () => string) => void, // invalid return type of callback
'invalid1': (data: string, cb: (string: string, number: number) => void) => void, // invalid number of callback arguments
}
type EventsWithCallback<E> = ???
type testFunction<Events> = (EventName: EventsWithCallback<Events>) => void
我在定义此 EventsWithCallback 类型时遇到问题。通过以下我得到错误:Type 'T[P]' does not satisfy the constraint '(...args: any[]) => void'
。这在某种程度上是合乎逻辑的。我尝试将 T 键入为 Record<string, (...args: any) => void>
但随后我匹配了所有字符串。
type Last<T extends any[]> = T extends [...any, infer Last] ? Last : any;
type EventsWithCallback<T> = keyof { [P in keyof T as Last<Parameters<T[P]>> extends Function ? P : never]: T[P] };
另外 extends Function
匹配任何函数和任何类型。
感谢您提供的任何帮助。我希望这个问题是可以理解的。
这个有用吗? Playground Link
interface Events {
'valid0': (data: string, cb: () => void) => void, // valid
'valid1': (data: number, cb: (data: string) => void) => void, // valid
'invalid0': (data: string, cb: () => string) => void, // invalid return type of callback
'invalid1': (data: string, cb: (string: string, number: number) => void) => void, // invalid number of callback arguments
}
type Last<T extends any[]> = T extends [...any, infer Last] ? Last : any
type EventsWithCallback<T> = {
[P in keyof T]:
T[P] extends (...args: infer A) => void // Check if it is a function
? Last<A> extends (arg: any) => any // Check if the last parameter is a function with single parameter
? ReturnType<Last<A>> extends void // Check if it returns void
? P // return the parameter name
: never
: never
: never
}[keyof T]
const testFunction = (eventName: EventsWithCallback<Events>) => {
eventName = 'invalid0' // error
eventName = 'invalid1' // error
eventName = 'valid0' // no error
eventName = 'valid1' // no error
}