Get Return Type
#
introductionImplement the built-in ReturnType<T>
generic without using it.
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"
#
start pointtsTry
/* _____________ 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 challengetsTry
/* _____________ 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);