Given the following exercise:
type MyReturnType<T> = T extends (...args: unknown[]) => infer R ? R: never
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<string, MyReturnType<(v: boolean) => 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
Even though the following types are inferred properly as I hover them,
MyReturnType<(v: boolean) => string>
,
MyReturnType<typeof fn>
,
MyReturnType<typeof fn1>
Expect
generic does not evaluate true. The rest except the above types are correctly identified.
Lastly, if I replace unknown[]
with any
, it is resolved again.
The implementation of Equal
of the library seems correct to me.
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
Could you help me to figure out what's going on here?