Skip to content

Commit e323ce3

Browse files
fix(overmind): resolve state of derived
1 parent a232b20 commit e323ce3

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ type State = {
77

88
describe('Derived', () => {
99
test('should instantiate app with derived state', () => {
10-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
11-
const state = {
10+
type State = {
11+
foo: string
12+
upperFoo: Derive<string, State>
13+
}
14+
const state: State = {
1215
foo: 'bar',
13-
upperFoo: derived,
16+
upperFoo: (state) => state.foo.toUpperCase(),
1417
}
1518
const config = {
1619
state,
@@ -20,7 +23,7 @@ describe('Derived', () => {
2023
state: typeof state
2124
}>
2225

23-
type Derive<Value> = TDerive<IApp, Value>
26+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
2427

2528
const app = new Overmind(config)
2629

@@ -30,14 +33,17 @@ describe('Derived', () => {
3033
test('should track derived state', () => {
3134
let renderCount = 0
3235
const changeFoo: Action = ({ state }) => (state.foo = 'bar2')
33-
34-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
36+
type State = {
37+
foo: string
38+
upperFoo: Derive<string, State>
39+
}
40+
const state: State = {
41+
foo: 'bar',
42+
upperFoo: (state) => state.foo.toUpperCase(),
43+
}
3544

3645
const config = {
37-
state: {
38-
foo: 'bar',
39-
upperFoo: derived,
40-
},
46+
state,
4147
actions: {
4248
changeFoo,
4349
},
@@ -50,7 +56,7 @@ describe('Derived', () => {
5056
actions: typeof config.actions
5157
}>
5258
type Action<Input = void> = TAction<IApp, Input>
53-
type Derive<Value> = TDerive<IApp, Value>
59+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
5460

5561
const app = new Overmind(config)
5662
function render() {
@@ -67,18 +73,21 @@ describe('Derived', () => {
6773
})
6874
test('should not require flush to flag as dirty', () => {
6975
expect.assertions(1)
76+
type State = {
77+
foo: string
78+
upperFoo: Derive<string, State>
79+
}
80+
const state: State = {
81+
foo: 'bar',
82+
upperFoo: (state) => state.foo.toUpperCase(),
83+
}
7084
const changeFoo: Action = ({ state }) => {
7185
state.foo = 'bar2'
7286
expect(state.upperFoo).toBe('BAR2')
7387
}
7488

75-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
76-
7789
const config = {
78-
state: {
79-
foo: 'bar',
80-
upperFoo: derived,
81-
},
90+
state,
8291
actions: {
8392
changeFoo,
8493
},
@@ -91,7 +100,7 @@ describe('Derived', () => {
91100
actions: typeof config.actions
92101
}>
93102
type Action<Input = void> = TAction<IApp, Input>
94-
type Derive<Value> = TDerive<IApp, Value>
103+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
95104

96105
const app = new Overmind(config)
97106

@@ -124,7 +133,7 @@ describe('Derived', () => {
124133
actions: typeof config.actions
125134
}>
126135
type Action<Input = void> = TAction<IApp, Input>
127-
type Derive<Value, Parent = any> = TDerive<IApp, Value, Parent>
136+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
128137

129138
const app = new Overmind(config)
130139

packages/node_modules/overmind/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export type TOperator<Input, Output, OperatorContext extends TContext<any>> = (
5050
final?: (err, Error, val?: TValueContext<OperatorContext, Output>) => void
5151
) => void
5252

53-
export type TDerive<App extends BaseApp, Value, Parent = any> = (
54-
parent: Parent,
53+
export type TDerive<App extends BaseApp, Value, Parent extends object = {}> = (
54+
parent: ResolveState<Parent>,
5555
state: App['state']
5656
) => Value
5757

0 commit comments

Comments
 (0)