Skip to content

Commit

Permalink
feat: flip
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhenpan committed Jul 18, 2024
1 parent a73a7ee commit 2b80f3c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/03192-medium-reverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Reverse<T extends Array<any>> = T extends [infer F, ...infer M, infer L]

/** 高赞答案 */
type Reverse1<T extends any[]> = T extends [infer F, ...infer Rest]
? [...Reverse<Rest>, F]
? [...Reverse1<Rest>, F]
: T

type A = Reverse<['a', 'b', 'c', 'd']>
type A = Reverse1<['a', 'b', 'c', 'd']>
35 changes: 35 additions & 0 deletions src/03196-medium-flip-arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'

type cases = [
Expect<Equal<FlipArguments<() => boolean>, () => boolean>>,
Expect<
Equal<FlipArguments<(foo: string) => number>, (foo: string) => number>
>,
Expect<
Equal<
FlipArguments<(arg0: string, arg1: number, arg2: boolean) => void>,
(arg0: boolean, arg1: number, arg2: string) => void
>
>
]

type errors = [
// @ts-expect-error
FlipArguments<'string'>,
// @ts-expect-error
FlipArguments<{ key: 'value' }>,
// @ts-expect-error
FlipArguments<['apple', 'banana', 100, { a: 1 }]>,
// @ts-expect-error
FlipArguments<null | undefined>
]

type Reverse<T extends any[]> = T extends [infer F, ...infer Rest]
? [...Reverse<Rest>, F]
: T

// ============= Your Code Here =============
type FlipArguments<T extends (...args: any[]) => any> = (
...args: Reverse<Parameters<T>>
) => ReturnType<T>

0 comments on commit 2b80f3c

Please sign in to comment.