-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
56 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |