Skip to content

Commit 7f70939

Browse files
feat(overmind): review and improve typing
BREAKING CHANGE: fixed issue with pipe typing and creating defaults on TOperator
1 parent 7202d1a commit 7f70939

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,46 +454,50 @@ export class Overmind<Config extends Configuration> implements Configuration {
454454
OPERATORS
455455
needs to be in this file for typing override to work
456456
*/
457-
export type Operator<Input, Output> = TOperator<TheConfig, Input, Output>
457+
export type Operator<Input = void, Output = Input> = TOperator<
458+
TheConfig,
459+
Input,
460+
Output
461+
>
458462

459-
export function pipe<Config extends Configuration, A, B, C>(
463+
export function pipe<Config extends Configuration, A, B>(
460464
aOperator: TOperator<Config, A, B>
461-
): TOperator<Config, A, C>
465+
): TOperator<Config, A, B>
462466

463-
export function pipe<Config extends Configuration, A, B, C, D>(
467+
export function pipe<Config extends Configuration, A, B, C>(
464468
aOperator: TOperator<Config, A, B>,
465469
bOperator: TOperator<Config, B, C>
466-
): TOperator<Config, A, D>
470+
): TOperator<Config, A, C>
467471

468-
export function pipe<Config extends Configuration, A, B, C, D, E>(
472+
export function pipe<Config extends Configuration, A, B, C, D>(
469473
aOperator: TOperator<Config, A, B>,
470474
bOperator: TOperator<Config, B, C>,
471475
cOperator: TOperator<Config, C, D>
472-
): TOperator<Config, A, E>
476+
): TOperator<Config, A, D>
473477

474-
export function pipe<Config extends Configuration, A, B, C, D, E, F>(
478+
export function pipe<Config extends Configuration, A, B, C, D, E>(
475479
aOperator: TOperator<Config, A, B>,
476480
bOperator: TOperator<Config, B, C>,
477481
cOperator: TOperator<Config, C, D>,
478482
dOperator: TOperator<Config, D, E>
479-
): TOperator<Config, A, F>
483+
): TOperator<Config, A, E>
480484

481-
export function pipe<Config extends Configuration, A, B, C, D, E, F, G>(
485+
export function pipe<Config extends Configuration, A, B, C, D, E, F>(
482486
aOperator: TOperator<Config, A, B>,
483487
bOperator: TOperator<Config, B, C>,
484488
cOperator: TOperator<Config, C, D>,
485489
dOperator: TOperator<Config, D, E>,
486490
eOperator: TOperator<Config, E, F>
487-
): TOperator<Config, A, G>
491+
): TOperator<Config, A, F>
488492

489-
export function pipe<Config extends Configuration, A, B, C, D, E, F, G, H>(
493+
export function pipe<Config extends Configuration, A, B, C, D, E, F, G>(
490494
aOperator: TOperator<Config, A, B>,
491495
bOperator: TOperator<Config, B, C>,
492496
cOperator: TOperator<Config, C, D>,
493497
dOperator: TOperator<Config, D, E>,
494498
eOperator: TOperator<Config, E, F>,
495499
fOperator: TOperator<Config, F, G>
496-
): TOperator<Config, A, H>
500+
): TOperator<Config, A, G>
497501

498502
export function pipe(...operators) {
499503
const instance = (err, context, next, final = next) => {
@@ -701,7 +705,7 @@ export function forEach<
701705
}
702706

703707
export function parallel<Input, Config extends Configuration = TheConfig>(
704-
operators: TOperator<Config, Input, any>[]
708+
...operators: TOperator<Config, Input>[]
705709
): TOperator<Config, Input, Input> {
706710
const instance = (err, context, next) => {
707711
if (err) next(err)

packages/node_modules/overmind/src/pipe.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('PIPE', () => {
6060
describe('OPERATORS', () => {
6161
test('map', () => {
6262
expect.assertions(1)
63-
const test: Operator<string, string> = pipe(
63+
const test: Operator<string> = pipe(
6464
map(({ value }) => value.toUpperCase())
6565
)
6666

@@ -70,7 +70,7 @@ describe('PIPE', () => {
7070
})
7171
test('map (async)', () => {
7272
expect.assertions(1)
73-
const test: Operator<string, string> = pipe(
73+
const test: Operator<string> = pipe(
7474
map(({ value }) => Promise.resolve(value.toUpperCase()))
7575
)
7676

@@ -81,7 +81,7 @@ describe('PIPE', () => {
8181
test('forEach', () => {
8282
expect.assertions(1)
8383
let runCount = 0
84-
const test: Operator<string[], string[]> = pipe(
84+
const test: Operator<string[]> = pipe(
8585
forEach((_, val, next) => {
8686
runCount++
8787
next(null, val)
@@ -95,17 +95,17 @@ describe('PIPE', () => {
9595
test('parallel', () => {
9696
expect.assertions(1)
9797
let runCount = 0
98-
const test: Operator<string, string> = pipe(
99-
parallel([
98+
const test: Operator<string> = pipe(
99+
parallel(
100100
(_, value, next) => {
101101
runCount++
102102
next(null, value)
103103
},
104104
(_, value, next) => {
105105
runCount++
106106
next(null, value)
107-
},
108-
])
107+
}
108+
)
109109
)
110110

111111
return createMockAction(test)('foo').then(() => {
@@ -114,7 +114,7 @@ describe('PIPE', () => {
114114
})
115115
test('filter - truthy', () => {
116116
expect.assertions(1)
117-
const test: Operator<string, string> = pipe(
117+
const test: Operator<string> = pipe(
118118
filter(({ value }) => value === 'foo'),
119119
map(({ value }) => value.toUpperCase())
120120
)
@@ -124,7 +124,7 @@ describe('PIPE', () => {
124124
})
125125
})
126126
test('filter - falsy', () => {
127-
const test: Operator<string, string> = pipe(
127+
const test: Operator<string> = pipe(
128128
filter(({ value }) => value === 'bar'),
129129
map(({ value }) => value.toUpperCase())
130130
)
@@ -135,7 +135,7 @@ describe('PIPE', () => {
135135
})
136136
test('fork', () => {
137137
expect.assertions(1)
138-
const test: Operator<string, string> = pipe(
138+
const test: Operator<string> = pipe(
139139
fork(() => 'foo', {
140140
foo: map(({ value }) => value.toUpperCase()),
141141
})
@@ -161,14 +161,14 @@ describe('PIPE', () => {
161161
test('wait', () => {
162162
expect.assertions(1)
163163
const runTime = Date.now()
164-
const test: Operator<string, string> = pipe(wait(500))
164+
const test: Operator<string> = pipe(wait(500))
165165
return createMockAction(test)('foo').then(() => {
166166
expect(Date.now() - runTime).toBeGreaterThanOrEqual(500)
167167
})
168168
})
169169
test('debounce', () => {
170170
expect.assertions(1)
171-
const test: Operator<string, string> = pipe(
171+
const test: Operator<string> = pipe(
172172
debounce(100),
173173
map(({ value }) => value.toUpperCase())
174174
)
@@ -190,7 +190,7 @@ describe('PIPE', () => {
190190
test('action (async)', () => {
191191
expect.assertions(2)
192192
let hasRun = false
193-
const test: Operator<string, string> = pipe(
193+
const test: Operator = pipe(
194194
action(({ value }) => Promise.resolve(value + '!!!')),
195195
action(() => (hasRun = true))
196196
)

packages/node_modules/overmind/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type TAction<Config extends Configuration, Value> = (
5959
context: TValueContext<Config, Value>
6060
) => any
6161

62-
export type TOperator<Config extends Configuration, Input, Output> = (
62+
export type TOperator<Config extends Configuration, Input, Output = Input> = (
6363
err: Error | null,
6464
val: TValueContext<Config, Input>,
6565
next: (err: Error | null, val?: TValueContext<Config, Output>) => void,

0 commit comments

Comments
 (0)