huihuilang53 / ts-challenges

ts类型体操刷题总结

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

parameters

huihuilang53 opened this issue · comments

commented

/*
3312 - Parameters

by midorizemi (@midorizemi) #简单 #infer #tuple #built-in

题目

实现内置的 Parameters 类型,而不是直接使用它,可参考TypeScript官方文档

例如:

const foo = (arg1: string, arg2: number): void => {}

type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]

在 Github 上查看:https://tsch.js.org/3312/zh-CN
*/

/* _____________ 你的代码 _____________ */

type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer x ) => any ? x : never

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

const foo = (arg1: string, arg2: number): void => {}
const bar = (arg1: boolean, arg2: { a: 'A' }): void => {}
const baz = (): void => {}

type cases = [
Expect<Equal<MyParameters, [string, number]>>,
Expect<Equal<MyParameters, [boolean, { a: 'A' }]>>,
Expect<Equal<MyParameters, []>>,
]

/* _____________ 下一步 _____________ /
/

分享你的解答:https://tsch.js.org/3312/answer/zh-CN
查看解答:https://tsch.js.org/3312/solutions
更多题目:https://tsch.js.org/zh-CN
*/