Typescript类型体操 - Parameters
题目
中文
实现内置的 Parameters<T> 类型,而不是直接使用它,可参考TypeScript官方文档。
例如:
const foo = (arg1: string, arg2: number): void => {}
type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]
English
Implement the built-in Parameters<T> generic without using it.
For example:
const foo = (arg1: string, arg2: number): void => {}
type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]
答案
type MyParameters<T extends (...args: any[]) => any> = T extends (...p: infer P) => any ? P : never;
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号