Skip to content

Commit cfeb65f

Browse files
fix(overmind): fix derived
BREAKING CHANGE: derived has changed typing
1 parent 562b58d commit cfeb65f

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ describe('Derived', () => {
1111
test('should instantiate app with derived state', () => {
1212
type State = {
1313
foo: string
14-
upperFoo: Derive<State, string>
14+
upperFoo: string
1515
}
1616
const state: State = {
1717
foo: 'bar',
18-
upperFoo: (state) => state.foo.toUpperCase(),
18+
upperFoo: derived((state: State) => state.foo.toUpperCase()),
1919
}
2020
const config = {
2121
state,
@@ -41,11 +41,11 @@ describe('Derived', () => {
4141
}
4242
type State = {
4343
foo: string
44-
upperFoo: Derive<State, string | null>
44+
upperFoo: string
4545
}
4646
const state: State = {
4747
foo: 'bar',
48-
upperFoo: (state) => state.foo.toUpperCase()
48+
upperFoo: derived((state: State) => state.foo.toUpperCase())
4949
}
5050

5151
const config = {
@@ -63,8 +63,6 @@ describe('Derived', () => {
6363
actions: typeof config.actions
6464
}
6565
interface Action<Input = void> extends IAction<Config, Input> {}
66-
interface Derive<Parent extends IState, Value>
67-
extends IDerive<Config, Parent, Value> {}
6866

6967
const app = new Overmind(config)
7068
const trackStateTree = app.getTrackStateTree()
@@ -91,14 +89,14 @@ describe('Derived', () => {
9189

9290
test('should dynamically add derived', () => {
9391
const addDerived: Action = ({ state }) => {
94-
state.upperFoo = derived<typeof state, string>((state) => state.foo.toUpperCase())
92+
state.upperFoo = derived((state: State) => state.foo.toUpperCase())
9593
}
9694
const changeFoo: Action = ({ state }) => {
9795
state.foo = 'bar2'
9896
}
9997
type State = {
10098
foo: string
101-
upperFoo: Derive<State, string> | null
99+
upperFoo: string
102100
}
103101
const state: State = {
104102
foo: 'bar',
@@ -146,16 +144,16 @@ describe('Derived', () => {
146144
}
147145
type State = {
148146
foo: string
149-
upperFoo: Derive<State, () => string>
147+
upperFoo: () => string
150148
}
151149
const state: State = {
152150
foo: 'bar',
153-
upperFoo: () =>
151+
upperFoo: derived(() =>
154152
function() {
155153
const state = this[PROXY_TREE].state
156154

157155
return state.foo.toUpperCase()
158-
},
156+
}),
159157
}
160158

161159
const config = {
@@ -172,8 +170,7 @@ describe('Derived', () => {
172170
actions: typeof config.actions
173171
}
174172
interface Action<Input = void> extends IAction<Config, Input> {}
175-
interface Derive<Parent extends IState, Value>
176-
extends IDerive<Config, Parent, Value> {}
173+
177174

178175
const app = new Overmind(config)
179176
const trackStateTree = app.getTrackStateTree()
@@ -196,11 +193,11 @@ describe('Derived', () => {
196193
}
197194
type State = {
198195
foo: string
199-
upperFoo: Derive<State, string>
196+
upperFoo: string
200197
}
201198
const state: State = {
202199
foo: 'bar',
203-
upperFoo: (state) => state.foo.toUpperCase(),
200+
upperFoo: derived((state: State) => state.foo.toUpperCase()),
204201
}
205202

206203
const config = {
@@ -217,8 +214,6 @@ describe('Derived', () => {
217214
actions: typeof config.actions
218215
}
219216
interface Action<Input = void> extends IAction<Config, Input> {}
220-
interface Derive<Parent extends IState, Value>
221-
extends IDerive<Config, Parent, Value> {}
222217

223218
const app = new Overmind(config)
224219
const trackStateTree = app.getTrackStateTree()
@@ -239,11 +234,11 @@ describe('Derived', () => {
239234
expect.assertions(1)
240235
type State = {
241236
foo: string
242-
upperFoo: Derive<State, string>
237+
upperFoo: string
243238
}
244239
const state: State = {
245240
foo: 'bar',
246-
upperFoo: (state) => state.foo.toUpperCase(),
241+
upperFoo: derived((state: State) => state.foo.toUpperCase()),
247242
}
248243
const changeFoo: Action = ({ state }) => {
249244
state.foo = 'bar2'

packages/node_modules/overmind/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export { SERIALIZE, rehydrate } from './rehydrate'
7474

7575
export { Statemachine, statemachine } from './statemachine'
7676

77-
export const derived = <S extends IState, V, C = Config>(cb: IDerive<C, S, V>): V => cb as any
77+
export const derived = <D extends IDerive<any, any, any>>(cb: D): D extends IDerive<any, any, infer O> ? O : never => cb as any
7878

7979
/** This type can be overwriten by app developers if they want to avoid
8080
* typing and then they can import `Action`, `Operation` etc. directly from
@@ -84,6 +84,8 @@ export interface Config {}
8484

8585
export interface Context extends IContext<Config> {}
8686

87+
export type RootState = Context["state"]
88+
8789
export interface Action<Value = void, ReturnValue = void>
8890
extends IAction<Config, Value, ReturnValue> {}
8991

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ITrackStateTree,
66
} from 'proxy-state-tree'
77

8-
import { IAction, IConfiguration, IDerive, IOperator, IState } from './types'
8+
import { IAction, IOperator, IState } from './types'
99

1010
export type SubType<Base, Condition> = Pick<
1111
Base,

0 commit comments

Comments
 (0)