millsp / medium

📰 Developer versions of my Medium.com articles
GNU Affero General Public License v3.0
126 stars 16 forks source link

there is wrong with CurryV5 #6

Open MuYunyun opened 3 years ago

MuYunyun commented 3 years ago

typescript version: 4.3.5

hi, it seems there is wrong with CurryV5 if using as follow:

- const toCurry09 = (name: string, age: number, single: boolean, ...nicknames: string[]) => true
+ const toCurry09 = (name: string, age: number, ...nicknames: string[]) => true

- const test47 = curried09('Jane', 26)(true, 'JJ', 'Jini') // boolean
- const test48 = curried09('Jane')(26, true, 'JJ', 'Jini') // boolean
- const test49 = curried09('Jane')(26)(true, 'JJ', 900000) // error 

+ const test47 = curried09('Jane', 26)('JJ', 'Jini') // boolean
+ const test48 = curried09('Jane')(26, 'JJ', 'Jini') // boolean
+ const test49 = curried09('Jane')(26)('JJ', 900000) // error

image

here is complete examples:

type CurryV5<P extends any[], R> =
  <T extends any[]>(...args: Cast<T, Partial<P>>) =>
    Drop<Length<T>, P> extends [any, ...any[]]
    ? CurryV5<Drop<Length<T>, P> extends infer DT ? Cast<DT, any[]> : never, R>
    : R

// When all the non-rest parameters are consumed, `Drop<Length<T>, P>` can only
// match `[...any[]]`. Thanks to this, we used `[any, ...any[]]` as a condition
// to end the recursion.

// Let's test it:
declare function curryV5<P extends any[], R>(f: (...args: P) => R): CurryV5<P, R>

const toCurry09 = (name: string, age: number, ...nicknames: string[]) => true
const curried09 = curryV5(toCurry09)

const test47 = curried09('Jane', 26)('JJ', 'Jini') // boolean
const test48 = curried09('Jane')(26, 'JJ', 'Jini') // boolean
const test49 = curried09('Jane')(26)('JJ', 900000) // error