Skip to content

Commit 746bacd

Browse files
authoredMay 16, 2020
Change the TypeScript types to allow no input (#10)
1 parent 5b5b58c commit 746bacd

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare namespace pPipe {
44
) => ReturnType | PromiseLike<ReturnType>;
55

66
type Pipeline<ValueType, ReturnType> = (
7-
value: ValueType
7+
value?: ValueType
88
) => Promise<ReturnType>;
99
}
1010

‎index.test-d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import {expectType} from 'tsd';
22
import pPipe = require('.');
33

44
const fn = async (string: string) => `${string} Unicorn`;
5+
const noInput = async () => 'called without input';
56

67
const identity = <T>(value: T) => value;
78
const toNumber = (string: string) => parseInt(string);
89
const toFixed = (number: number) => number.toFixed(2);
910

1011
expectType<Promise<string>>(pPipe(fn)('❤️'));
12+
expectType<Promise<string>>(pPipe(noInput)());
1113
expectType<Promise<string>>(pPipe(toNumber, toFixed)('❤️'));
1214

1315
expectType<Promise<string>>(pPipe(fn, identity, identity)('❤️'));
@@ -47,6 +49,14 @@ expectType<Promise<string>>(
4749
)('❤️')
4850
);
4951

52+
expectType<Promise<string>>(
53+
pPipe(
54+
noInput,
55+
identity,
56+
identity
57+
)()
58+
);
59+
5060
expectType<unknown>(
5161
pPipe(
5262
fn,
@@ -61,12 +71,20 @@ expectType<unknown>(
6171
identity
6272
)('❤️')
6373
);
74+
expectType<unknown>(
75+
pPipe(
76+
noInput,
77+
identity,
78+
identity
79+
)()
80+
);
6481

6582
// "Complex" examples
6683
const byPowerOfTwo = (number: number) => number ** 2;
6784
const asResult = async <T>(result: T) => ({result});
6885
const either = async (number: number) => (number > 2 ? number : '🤪');
6986
const count = (number: number) => [...Array(number).keys()];
87+
const fetchNumber = async () => 2;
7088

7189
expectType<Promise<{result: string}>>(
7290
pPipe(byPowerOfTwo, toFixed, asResult)(2)
@@ -75,3 +93,6 @@ expectType<Promise<{result: number | string}>>(
7593
pPipe(byPowerOfTwo, either, asResult)(2)
7694
);
7795
expectType<Promise<number[]>>(pPipe(byPowerOfTwo, count)(2));
96+
expectType<Promise<{result: number | string}>>(
97+
pPipe(fetchNumber, either, asResult)()
98+
);

‎test.js

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import pPipe from '.';
55
const addUnicorn = async string => `${string} Unicorn`;
66
const addRainbow = string => Promise.resolve(`${string} Rainbow`);
77
const addNonPromise = string => `${string} Foo`;
8+
const fetchString = () => 'without input';
89

910
test('main', async t => {
1011
const single = pPipe(addUnicorn);
@@ -65,3 +66,9 @@ test('reuse pipe', async t => {
6566
t.is(await task('❤️'), '❤️ Unicorn');
6667
t.is(await task('❤️'), '❤️ Unicorn');
6768
});
69+
70+
test('calls function without input', async t => {
71+
const withoutInput = pPipe(fetchString, addUnicorn);
72+
73+
t.is(await withoutInput(), 'without input Unicorn');
74+
});

0 commit comments

Comments
 (0)
Please sign in to comment.