Dear,大家好,我是“前端小鑫同学”,😇长期从事前端开发,安卓开发,热衷技术,在编程路上越走越远~
题目地址:2-medium-return-type
如上图所示,我们需要设计一个通用了类型工具还提取函数类型的返回类型,功能同内置的ReturnType。
给出的测试用例挺多但是没有特殊需要说明的,我们只需要通过设计的类型工具取出到通过Equal来进行比较即可。
/* _____________ 测试用例 _____________ */
import { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<string, MyReturnType<() => string>>>,
Expect<Equal<123, MyReturnType<() => 123>>>,
Expect<Equal<ComplexObject, MyReturnType<() => ComplexObject>>>,
Expect<Equal<Promise<boolean>, MyReturnType<() => Promise<boolean>>>>,
Expect<Equal<() => 'foo', MyReturnType<() => () => 'foo'>>>,
Expect<Equal<1 | 2, MyReturnType<typeof fn>>>,
Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>,
]
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
/* _____________ 你的代码 _____________ */
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
复制代码
接下来的一题是:【类型挑战】实现 Omit,难度⭐️⭐️