跳到主要内容

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

起点

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);
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);
take the challenge

解决方法

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