Skip to content

Commit fcf5c66

Browse files
fix(overmind): fix typing issues with fork
1 parent 22a2efd commit fcf5c66

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,11 +1413,11 @@ export function tryCatch<
14131413

14141414
export function fork<
14151415
Input,
1416-
Paths extends { [key: string]: IOperator<ThisConfig, Input, any> },
1416+
Paths extends { [key: string]: IOperator<ThisConfig, any, any> },
14171417
ThisConfig extends IConfiguration = Config
14181418
>(
14191419
operation: (context: IContext<ThisConfig>, value: Input) => keyof Paths,
1420-
paths: Paths
1420+
paths: Paths & { [N in keyof Paths]: IOperator<ThisConfig, Input, any> }
14211421
): IOperator<ThisConfig, Input, Input> {
14221422
return createOperator<ThisConfig>(
14231423
'fork',

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,24 @@ describe('OPERATORS', () => {
195195

196196
test('fork', () => {
197197
expect.assertions(1)
198+
enum Key {
199+
Foo = 'foo',
200+
}
198201
const test: Operator<string> = pipe(
199-
fork(() => 'foo', {
200-
foo: pipe(
201-
map((_, value) => value.toUpperCase()),
202+
fork(() => Key.Foo, {
203+
[Key.Foo]: pipe(
204+
map((_, value) => {
205+
return value.toUpperCase()
206+
}),
202207
mutate(({ state }, value) => (state.foo = value))
203208
),
204209
})
205210
)
206211

207-
const state = {
208-
foo: 'bar',
209-
}
210212
const config = {
211-
state,
213+
state: {
214+
foo: 'bar',
215+
},
212216
actions: {
213217
test,
214218
},

packages/overmind-website/api/operators.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ Allows you to execute an operator/pipe based on the matching key.
5252
h(Example, { name: "api/operators_operator_fork" })
5353
```
5454

55+
```marksy
56+
h(TypescriptNotice, null, "You have to use ENUM for these keys for the typing to work")
57+
```
58+
5559
## map
5660
Returns a new value to the pipe. If the value is a promise it will wait until promise is resolved.
5761

packages/overmind-website/examples/api/operators_operator_fork.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export const forkUserType: (paths: { [key: string]: Operator<User> }) => Operato
1818
code: `
1919
import { Operator, pipe } from 'overmind'
2020
import * as o from './operators'
21+
import { UserType } from './state'
2122
2223
export const getUser: Operator<string, User> = pipe(
2324
o.getUser(),
2425
o.forkUserType({
25-
admin: o.doThis(),
26-
superuser: o.doThat()
26+
[UserType.ADMIN]: o.doThis(),
27+
[UserType.SUPERUSER]: o.doThat()
2728
})
2829
)
2930
`,

0 commit comments

Comments
 (0)