Skip to main content

Parameters

introduction#

Implement the built-in Parameters generic without using it.

View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
type MyParameters<T extends (...args: any[]) => any> = any;
 
/* _____________ Test Cases _____________ */
const foo = (arg1: string, arg2: number): void => {};
const bar = (arg1: boolean, arg2: { a: 'A' }): void => {};
const baz = (): void => {};
 
type cases = [
Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<MyParameters<typeof baz>, []>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
];
Try
ts
/* _____________ Your Code Here _____________ */
type MyParameters<T extends (...args: any[]) => any> = any;
 
/* _____________ Test Cases _____________ */
const foo = (arg1: string, arg2: number): void => {};
const bar = (arg1: boolean, arg2: { a: 'A' }): void => {};
const baz = (): void => {};
 
type cases = [
Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<MyParameters<typeof baz>, []>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
];
Try
take the challenge

my solution#

Spoiler warning // Click to reveal answer
ts
type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer T) => any
? T
: never;
Try
ts
type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer T) => any
? T
: never;
Try
view more solutions