Skip to main content

Get Return Type

introduction#

Implement 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"
ts
const fn = (v: boolean) => {
if (v) return 1;
else return 2;
};
type a = MyReturnType<typeof fn>; // should be "1 | 2"
View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
type MyReturnType<T> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
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<typeof fn>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
];
 
type ComplexObject = {
a: [12, 'foo'];
bar: 'hello';
prev(): number;
};
 
const fn = (v: boolean) => (v ? 1 : 2);
const fn1 = (v: boolean, w: any) => (v ? 1 : 2);
Try
ts
/* _____________ Your Code Here _____________ */
type MyReturnType<T> = any;
 
/* _____________ Test Cases _____________ */
type cases = [
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<typeof fn>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
];
 
type ComplexObject = {
a: [12, 'foo'];
bar: 'hello';
prev(): number;
};
 
const fn = (v: boolean) => (v ? 1 : 2);
const fn1 = (v: boolean, w: any) => (v ? 1 : 2);
Try
take the challenge

my solutions#

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