type-challenges / type-challenges

Collection of TypeScript type challenges with online judge
https://tsch.js.org/
MIT License
42.98k stars 4.67k forks source link

Issue with challenge 00017-hard-currying-1 #13930

Open biggyspender opened 2 years ago

biggyspender commented 2 years ago

https://github.com/type-challenges/type-challenges/blob/main/questions/00017-hard-currying-1/README.md

If we declare a function:

const f = () => true;

The return type is widened to be:

const f = () => boolean;

Playground Link

So, is it right that the expectation for curried function does NOT expect this widening?

However, this touches on an edge-case in TypeScript where the function may or may not be widened according to the constraints placed on the generic type that represents it.

So, if the generic type is:

declare function Currying<Fn>(fn: Fn): ...

the function return type ReturnType<Fn> is not widened...

however, if the generic type is (which, to my thinking is the preferred solution)

declare function Currying<Fn extends (...args: any) => any>(fn: Fn): ...

then the function return type ReturnType<Fn> is widened to boolean.

See this issue that I opened in SO that better describes the issue.

So, is this expectation correct?

  Expect<Equal<
    typeof curried1, (a: string) => (b: number) => (c: boolean) => true
  >>

or should it be

  Expect<Equal<
    typeof curried1, (a: string) => (b: number) => (c: boolean) => boolean
  >>

?

Compare this solution with this one

cpWhitecat commented 2 years ago

I have same issue too , I also used returnType, however result type always reference a boolean , maybe it's type widening I think about you need to know type widening and type covariant My knowlage is limited ,I can't have a good explanation to you about those

declare function Currying<Fn extends (...args: any) => any>(fn: Fn): ...

const result4 = Currying((a: string, b: number) => 123456) 
type A = typeof result4 //result reference number

const result5 = Currying((a: string, b: number) => 'ljsfoe') 
type B = typeof result5 //result reference string

they all type widening

cpWhitecat commented 2 years ago

You can see extreme Currying2 challenge playground the test type is Boolean