From 2b80f3cb6a3fce29df120c74a6fc349c0c903fb0 Mon Sep 17 00:00:00 2001 From: zhouzhenpan Date: Thu, 18 Jul 2024 14:52:18 +0800 Subject: [PATCH] feat: flip --- src/03192-medium-reverse.ts | 4 ++-- src/03196-medium-flip-arguments.ts | 35 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/03196-medium-flip-arguments.ts diff --git a/src/03192-medium-reverse.ts b/src/03192-medium-reverse.ts index 4bfc02d..c2ff6e7 100644 --- a/src/03192-medium-reverse.ts +++ b/src/03192-medium-reverse.ts @@ -23,7 +23,7 @@ type Reverse> = T extends [infer F, ...infer M, infer L] /** 高赞答案 */ type Reverse1 = T extends [infer F, ...infer Rest] - ? [...Reverse, F] + ? [...Reverse1, F] : T -type A = Reverse<['a', 'b', 'c', 'd']> +type A = Reverse1<['a', 'b', 'c', 'd']> diff --git a/src/03196-medium-flip-arguments.ts b/src/03196-medium-flip-arguments.ts new file mode 100644 index 0000000..a844363 --- /dev/null +++ b/src/03196-medium-flip-arguments.ts @@ -0,0 +1,35 @@ +// ============= Test Cases ============= +import type { Equal, Expect } from './test-utils' + +type cases = [ + Expect boolean>, () => boolean>>, + Expect< + Equal 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 +] + +type Reverse = T extends [infer F, ...infer Rest] + ? [...Reverse, F] + : T + +// ============= Your Code Here ============= +type FlipArguments any> = ( + ...args: Reverse> +) => ReturnType