forked from sindresorhus/p-pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
77 lines (70 loc) · 1.77 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
import {expectType} from 'tsd';
import pPipe = require('.');
const fn = async (string: string) => `${string} Unicorn`;
const identity = <T>(value: T) => value;
const toNumber = (string: string) => parseInt(string);
const toFixed = (number: number) => number.toFixed(2);
expectType<Promise<string>>(pPipe(fn)('❤️'));
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<unknown>(
pPipe(
fn,
identity,
identity,
identity,
identity,
identity,
identity,
identity,
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()];
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));