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;

在线演示

posted @ 2022-09-05 20:39  Laggage  阅读(230)  评论(0编辑  收藏  举报