Skip to content

Commit

Permalink
Fix types of function-flip (#438)
Browse files Browse the repository at this point in the history
In the previous version, the type of flip((x: string, y: number) => any) was incorrectly inferred as (x: string, y: number) => any.
  • Loading branch information
Masa-Shin authored Apr 9, 2022
1 parent 52cbafb commit f142890
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
10 changes: 9 additions & 1 deletion packages/function-flip/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Definitions by: Aryaman1706 <https://github.com/Aryaman1706>
declare function flip<T = (...args: any[] | never) => any>(fn: T): T;
// Modified by: Masa-Shin <https://github.com/Masa-Shin>

declare function flip<T extends (...args: any[]) => any>(fn: T):
T extends () => any
? T
: T extends (arg0: infer Arg0, arg1: infer Arg1, ...rest: infer Rest) => infer R
? (...args: [Arg1, Arg0, ...Rest]) => R
: never;

export default flip;
64 changes: 47 additions & 17 deletions packages/function-flip/index.tests.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,65 @@
import flip from './index';

const fn = (...args: never) => {
return 21;
const getOne = (...args: never) => {
return 1;
}
const addOne = (n: number) => {
return n + 1
}
const repeat = (str: string, n: number) => {
return str.repeat(n)
}
const getFormattedPrice = (amount: number, unit: string, includeTax: boolean) => {
const tax = includeTax ? 1.1 : 1
return `${unit}${amount * tax}`
}

// OK
flip(getOne)()

flip(addOne)(undefined, -1)

flip(repeat)(3, "hello")

flip(getFormattedPrice)("$", 1000, true)

flip(console.log)(1, 2, 3);
flip(console.log)();
flip<(...args: any[]) => void>(console.log)(1, 2, 3);
flip<typeof console.log>(console.log)(1, 2, 3);
flip(Array)(1, 2);
flip<typeof Array>(Array)(1, 2);
flip<(...args: string[]) => string[]>(Array)("Hello", "World");
flip<any>(fn)(34);
flip(console.log)(1, "a");
flip(console.log)(1, 2, 3);
flip(console.log)(true, "a", 3, 4, 5);

// Not OK

// @ts-expect-error
flip(null)(1, 2, 3);
flip(getOne)(1)

// @ts-expect-error
flip(addOne)()
// @ts-expect-error
flip(addOne)(1, 2, 3)
// @ts-expect-error

flip(repeat)()
// @ts-expect-error
flip(repeat)(1)
// @ts-expect-error
flip(repeat)(1, "hello", true)
// @ts-expect-error
flip(repeat)(1, 2)
// @ts-expect-error
flip(repeat)("hello", "world")

// @ts-expect-error
flip(1)(1, 2, 3);
flip(getFormattedPrice)()
// @ts-expect-error
flip('Hello World')(1, 2, 3);
flip(getFormattedPrice)("$")
// @ts-expect-error
flip()(1, 2, 3);
flip(getFormattedPrice)("$", 1000)
// @ts-expect-error
flip<(...args: never[]) => void>(console.log)(1, 2, 3);
flip(getFormattedPrice)("$", 1000, true, undefined)
// @ts-expect-error
flip<(...args: number[]) => string>(Array)(1, 2);
flip(getFormattedPrice)(undefined, 10000, true)
// @ts-expect-error
flip(fn)(34)
flip(getFormattedPrice)("$", undefined, true)
// @ts-expect-error
flip<(...args: number[]) => number>(fn)(34)
flip(getFormattedPrice)("$", 10000, undefined)

0 comments on commit f142890

Please sign in to comment.