Parameters
介绍
实现内置的Parameters泛型而不使用它.
View on GitHubstart point
ts
/* _____________ Your Code Here _____________ */typeMyParameters <T extends (...args : any[]) => any> = any;/* _____________ Test Cases _____________ */constfoo = (arg1 : string,arg2 : number): void => {};constbar = (arg1 : boolean,arg2 : {a : 'A' }): void => {};constbaz = (): void => {};typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeoffoo >, [string, number]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeofbar >, [boolean, {a : 'A' }]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeofbaz >, []>>];
take the challengets
/* _____________ Your Code Here _____________ */typeMyParameters <T extends (...args : any[]) => any> = any;/* _____________ Test Cases _____________ */constfoo = (arg1 : string,arg2 : number): void => {};constbar = (arg1 : boolean,arg2 : {a : 'A' }): void => {};constbaz = (): void => {};typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeoffoo >, [string, number]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeofbar >, [boolean, {a : 'A' }]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyParameters <typeofbaz >, []>>];
my solution
Spoiler warning // Click to reveal answer
ts
typeMyParameters <T extends (...args : any[]) => any> =T extends (...args : inferT ) => any?T : never;
ts
typeMyParameters <T extends (...args : any[]) => any> =T extends (...args : inferT ) => any?T : never;