03196-medium-flip-arguments

Back

type FlipArguments<T extends Function> = T extends (...p: infer args) => infer R ? (...p: args) => R : never;

Solution by wendao-liu #35056

type Reverse<T extends unknown[]> = T extends [infer F, ...infer R] ? [...Reverse<R>, F] : [];

type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: infer P) => infer U 
? (...args: Reverse<P>) => U
: never;

Solution by devshinthant #34953

type Reverse<T extends any[]> = T extends [infer First, ...infer Rest] ? [...Reverse<Rest>, First] : []
type FlipArguments<T extends  (...args: any[]) => any> = T extends (...args: infer Args) => infer U ? (...args:Reverse<Args>) => U : never

Solution by maximallain #34779

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

type FlipArguments<T extends (...args: any) => any> = (...args: Reverse<Parameters<T>>) => ReturnType<T>

Solution by Jayce-liang #34768

type Reverse<A extends any[], U extends any[] = []> = A extends [...infer rest, infer R] ? Reverse<rest, [...U, R]> : U
type FlipArguments<T extends Function> = T extends (...args: infer P) => infer R ? (...args: Reverse<P>) => R : never

Solution by ouzexi #34058

3196 - Flip Arguments

by jiangshan (@jiangshanmeta) #medium #arguments

Question

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

View on GitHub: https://tsch.js.org/3196

...


type Reverse<A extends unknown[]> = A extends [infer L, ...infer R]
  ? [...Reverse<R>, L]
  : A

type FlipArguments<T extends (...args: any) => any> = T extends (...args: [...infer A]) => ReturnType<T>
  ? (...args: Reverse<A>) => ReturnType<T>
  : T

Solution by veralex #33811

type Reversed<A> = A extends [infer F, ...infer Rest] ? [...Reversed<Rest>, F] : [];

type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: infer A) => infer R 
  ? (...args: Reversed<A>) => R
  : never;

Solution by kai-phan #32517

type FlipArguments<T extends (...args: any[]) => any, U extends any[] = []> = T extends (
	...args: infer Args
) => infer RT
	? Args extends [...infer F, infer R]
		? FlipArguments<(...args: F) => RT, [...U, R]>
		: (...args: U) => RT
	: T;

Solution by gasmg #32209

type Reverse<T extends unknown[]> = T extends [... infer R, infer L] ? [L, ...Reverse<R>] : T;
type FlipArguments<T extends Function> = T extends (...args: infer A) => infer R ? (...args: Reverse<A>) => R : never;

Solution by ricky-fn #31919

type Reverse<T extends unknown[]> = T extends [infer F, ...infer E] ? [...Reverse<E>, F]:[]
type FlipArguments<T extends (...args:any[]) => any> = T extends (...args:infer P) => infer U ? (...args: Reverse<P>)=>U : never

Solution by Zhen-code #31882

type Reverse<T extends any[], U extends any[] = []> = T extends [infer A, ...infer B] ? Reverse<B, [...U, A]> : U
type FlipArguments<T extends Function> = T extends (...arg: infer B) => infer C ? (...arg0: Reverse<B>) => C : never

Solution by dreamluo-plus #30620

// your answers

type ReversTypeArr<T extends  unknown[]> =T extends [infer F, ...infer Rest]  ? [...ReversTypeArr<Rest> , F] : 
[]

type FlipArguments<T extends (...ags: any)=> any  >= T extends (...args: infer R) => infer U ? (...args: ReversTypeArr<R>)=> U : never

Solution by kerolossamir165 #29523

Solution:

type ReverseArray<Arr extends any[]> = Arr extends [infer First, ...infer Last] ? [...ReverseArray<Last>, First] : Arr;
type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: [...infer A]) => infer R 
? (...args: ReverseArray<A>) => R : never;

Solution by DmitriiBr #29520

type Reverse<T extends any[]> = T extends [infer A, ...infer B] ? [...Reverse<B>, A]: [];

type R = Reverse<[1, 2, 3, 4]>;

type FlipArguments<T extends Function> = T extends (...args: infer E) => any ?
    (...args: Reverse<E>) => any : never;

type Flipped = FlipArguments<(arg0: string, arg1: number, arg2: boolean) => void>;

const flipped: Flipped = (a: boolean, b: number, c: string) => {

};

Solution by sundial-dreams #29493

// your answers
type TransferToRe<T extends any[] ,Arr extends any[]=[]> =T["length"] extends 0? Arr: T extends [infer A,...infer B]? TransferToRe<B,[A,...Arr]>:Arr
type FlipArguments<T extends (...vaules:any[])=>any,Arr extends any[]=[]> =T extends (...vaules:infer A)=>infer C
    ? (...vaules:TransferToRe<A>)=>C:never

Solution by sunhk123456 #29190

type Reverse<T extends unknown[]> = T extends [infer F, ...infer R] ? [...Reverse<R>, F] : T;

type_ FlipArguments<T extends (...args: any) => unknown> = T extends ( ...args: infer Args ) => infer Result ? (...args: Reverse<Args>) => Result : T;

Solution by DoubleWoodLin #28701

// your answers
type Reverse<T extends unknown[]> = T extends [infer F, ...infer R] ? [...Reverse<R>, F] : T;
type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: infer P) => infer U 
? (...args: Reverse<P>) => U
: never;

Solution by daiki-skm #28235


type Push<T, U> = T extends any[] ? [...T, U] : never;

type FlipArguments<T extends (...args:any) => any, U extends any[] = []> = 
  T extends (...args: infer F) => infer R ? 
  F extends [...infer O, infer L] ?
  FlipArguments<(...args: O) => R, Push<U, L>> : (...args: U) => R : never;

Solution by 8471919 #27732

type Reverse<T extends any[], P extends any[] = []> = T extends [infer L, ...infer R] ? Reverse<R, [L, ...P]>: P

type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: infer A) => infer R ? (...args: Reverse<A>) => R: never

Solution by isntkyu #27713

type Reverse<T extends unknown[]> = T extends [infer First, ...infer Tail] ? [...Reverse<Tail>, First] : []
    
type FlipArguments<T extends (...args: any[]) => any > = 
  T extends (...args: infer P) => infer R ? (...args: Reverse<P>) => R : never

Solution by jjswifty #27636

type Reverse<T> = T extends [...infer Pre, infer Next] ? [Next, ...Reverse<Pre>] : []
type FlipArguments<T> = T extends (...args: infer Args) => infer Return ? (...args: Reverse<Args>) => Return : T

Solution by jiechliu #27451

type ReverseArg<T,R extends any[] = []> = T extends [...infer A, infer B] ? ReverseArg<A, [...R, B]>:R
type FlipArguments<T extends Function> = T extends (...args: infer A) => infer B ? (...args:ReverseArg<A>) => B : never

Solution by ZhipengYang0605 #27310

type Reverse<T extends unknown[]> = T extends [...infer Rest, infer Last]
  ? [Last, ...Reverse<Rest>]
  : T

type FlipArguments<T extends (...args: never[]) => unknown> =
  T extends (...args: infer P) => infer R
    ? (...args: Reverse<P>) => R
    : never

Solution by HubooDeclan #27031

type Reverse<T extends unknown[]> = T extends [...infer R, infer L] ? R extends [] ? T : [L, ...Reverse<R>] : [];
type FlipArguments<T> = T extends (...args: infer P) => infer R ? (...args: Reverse<P>) => R : never;

Solution by smileboyi #26965

type Reverse<T extends unknown[]> = T extends [...infer V, infer R]
  ? [R, ...Reverse<V>]
  : T;

type FlipArguments<T extends (...args: any) => any> = T extends (
  ...args: infer V
) => infer R
  ? (...args: Reverse<V>) => R
  : never;

Solution by ryuji-1to #26884

type Reverse<T extends unknown[]> = T extends [infer First, ...infer Rest] ? [...Reverse<Rest>, First] : T;
type FlipArguments<T> = T extends (...args: infer Args) => infer ReturnType
  ? (...args: Reverse<Args>) => ReturnType
  : never;

Solution by rldnd #26857

// your answers
type Reverse<T extends unknown[]> = T extends [...infer R, infer L] ? [L, ...Reverse<R>] : T;
type FlipArguments<T> = T extends (...arg: infer Arg) => infer R ?  (...arg: Reverse<Arg>) => R : T

Solution by CheolMinBae #26679

type Reverse<T extends unknown[]> = T extends [infer First, ...infer Other] ? [...Reverse<Other>, First] : [];
type FlipArguments<T extends (...arg: any) => any> = T extends (...arg: infer First) => infer Other ? (...arg: Reverse<First>) => Other : never; 

Solution by jsujeong #26676

type ReverseArgTpyes<Types extends any[]> = Types extends [...infer F, infer R] ? [R, ...ReverseArgTpyes<F>] : Types
type FlipArguments<T extends (...args: any[]) => any> = T extends (...args: infer Types) => infer U
  ? (...args: ReverseArgTpyes<Types>) => U
  : never

Solution by HaoxueAllen #26535

type FlipArguments<T extends (...args: never[]) => unknown> = (...args: [...Flip<Parameters<T>>]) => ReturnType<T>
type Flip<T extends unknown[]> = T extends [] ? [] : T extends [infer Arg, ...infer Args] ? [...Flip<Args>, Arg] : never

Solution by ScarboroughCoral #26313