-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathNoInfer.ts
35 lines (32 loc) · 1.02 KB
/
NoInfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Explain to TS which function parameter has priority for generic inference
* @param A to de-prioritize
* @returns `A`
* @example
* ```ts
* import {F} from 'ts-toolbelt'
*
* const fn0 = <A extends any>(a0: A, a1: F.NoInfer<A>): A => {
* return {} as unknown as A // just for the example
* }
*
* const fn1 = <A extends any>(a0: F.NoInfer<A>, a1: A): A => {
* return {} as unknown as A // just for the example
* }
*
* const fn2 = <A extends any>(a0: F.NoInfer<A>, a1: F.NoInfer<A>): A => {
* return {} as unknown as A // just for the example
* }
*
* const test0 = fn0('b', 'a') // error: infer priority is `a0`
* const test1 = fn1('b', 'a') // error: infer priority is `a1`
* const test2 = fn2('b', 'a') // works: infer priority is `a0` | `a1`
* ```
* @see https://stackoverflow.com/questions/56687668
*/
export type NoInfer<A extends any> =
[A][A extends any ? 0 : never]
/*
* https://github.com/microsoft/TypeScript/issues/14829#issuecomment-322267089
* https://stackoverflow.com/questions/56687668
*/