Implement the type version of lodash's _.flip
.
Type FlipArguments<T>
requires function type T
and returns a new function type which has the same return type of T but reversed parameters.
For example:
type Flipped = FlipArguments<(arg0: string, arg1: number, arg2: boolean) => void>
// (arg0: boolean, arg1: number, arg2: string) => void
/* _____________ Your Code Here _____________ */
type Reverse<T extends any[]> = T extends [...infer A, infer B] ? [B, ...Reverse<A>] : []
type FlipArguments<T extends (...arg: any[]) => any> = T extends (...args: infer Args) => infer RT ? (...r: Reverse<Args>) => RT : never;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/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>,
]
标签:Typescript,FlipArguments,56,number,ts,boolean,Arguments,type,infer From: https://www.cnblogs.com/Answer1215/p/16803772.html