Get Return Type
介绍
实现内置的ReturnType<T>
泛型而不用它.
For example
ts
const fn = (v: boolean) => {if (v) return 1;else return 2;};type a = MyReturnType<typeof fn>; // should be "1 | 2"
View on GitHubts
const fn = (v: boolean) => {if (v) return 1;else return 2;};type a = MyReturnType<typeof fn>; // should be "1 | 2"
起点
ts
/* _____________ Your Code Here _____________ */typeMyReturnType <T > = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <string,MyReturnType <() => string>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <123,MyReturnType <() => 123>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ComplexObject ,MyReturnType <() =>ComplexObject >>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Promise <boolean>,MyReturnType <() =>Promise <boolean>>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <() => 'foo',MyReturnType <() => () => 'foo'>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <1 | 2,MyReturnType <typeoffn >>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <1 | 2,MyReturnType <typeoffn1 >>>];typeComplexObject = {a : [12, 'foo'];bar : 'hello';prev (): number;};constfn = (v : boolean) => (v ? 1 : 2);constfn1 = (v : boolean,w : any) => (v ? 1 : 2);
take the challengets
/* _____________ Your Code Here _____________ */typeMyReturnType <T > = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <string,MyReturnType <() => string>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <123,MyReturnType <() => 123>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <ComplexObject ,MyReturnType <() =>ComplexObject >>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Promise <boolean>,MyReturnType <() =>Promise <boolean>>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <() => 'foo',MyReturnType <() => () => 'foo'>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <1 | 2,MyReturnType <typeoffn >>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <1 | 2,MyReturnType <typeoffn1 >>>];typeComplexObject = {a : [12, 'foo'];bar : 'hello';prev (): number;};constfn = (v : boolean) => (v ? 1 : 2);constfn1 = (v : boolean,w : any) => (v ? 1 : 2);
解决方法
Spoiler warning // Click to reveal answer
ts
typeMyReturnType <T > =T extends (...args : any[]) => inferR ?R : never;
ts
typeMyReturnType <T > =T extends (...args : any[]) => inferR ?R : never;