-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.test-d.ts
98 lines (90 loc) · 2.21 KB
/
index.test-d.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {expectType} from 'tsd';
import pPipe from './index.js';
const fn = async (string: string) => `${string} Unicorn`;
const noInput = async () => 'called without input';
const identity = <T>(value: T) => value;
const toNumber = (string: string) => Number.parseInt(string, 10);
const toFixed = (number: number) => number.toFixed(2);
expectType<Promise<string>>(pPipe(fn)('❤️'));
expectType<Promise<string>>(pPipe(noInput)());
expectType<Promise<string>>(pPipe(toNumber, toFixed)('❤️'));
expectType<Promise<string>>(pPipe(fn, identity, identity)('❤️'));
expectType<Promise<string>>(pPipe(fn, identity, identity, identity)('❤️'));
expectType<Promise<string>>(
pPipe(fn, identity, identity, identity, identity)('❤️')
);
expectType<Promise<string>>(
pPipe(fn, identity, identity, identity, identity, identity)('❤️')
);
expectType<Promise<string>>(
pPipe(fn, identity, identity, identity, identity, identity, identity)('❤️')
);
expectType<Promise<string>>(
pPipe(
fn,
identity,
identity,
identity,
identity,
identity,
identity,
identity
)('❤️')
);
expectType<Promise<string>>(
pPipe(
fn,
identity,
identity,
identity,
identity,
identity,
identity,
identity,
identity
)('❤️')
);
expectType<Promise<string>>(
pPipe(
noInput,
identity,
identity
)()
);
expectType<Promise<unknown>>(
pPipe(
fn,
identity,
identity,
identity,
identity,
identity,
identity,
identity,
identity,
identity
)('❤️')
);
expectType<Promise<string>>(
pPipe(
noInput,
identity,
identity
)()
);
// "Complex" examples
const byPowerOfTwo = (number: number) => number ** 2;
const asResult = async <T>(result: T) => ({result});
const either = async (number: number) => (number > 2 ? number : '🤪');
const count = (number: number) => [...Array(number).keys()]; // eslint-disable-line unicorn/new-for-builtins
const fetchNumber = async () => 2;
expectType<Promise<{result: string}>>(
pPipe(byPowerOfTwo, toFixed, asResult)(2)
);
expectType<Promise<{result: number | string}>>(
pPipe(byPowerOfTwo, either, asResult)(2)
);
expectType<Promise<number[]>>(pPipe(byPowerOfTwo, count)(2));
expectType<Promise<{result: number | string}>>(
pPipe(fetchNumber, either, asResult)()
);