Skip to content

Commit 4366c23

Browse files
fix(overmind): try to fix typing issue with rehydrate classes
1 parent 0f2aedf commit 4366c23

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function createDefaultOvermind() {
5858
}
5959
const rehydrateAction: Action<any> = ({ state }, newState) => {
6060
rehydrate(state, newState, {
61-
value: StateValue.fromJSON
61+
value: StateValue.fromJSON,
6262
})
6363
}
6464
const actions = {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('REHYDRATE', () => {
3131
foo: 'bar2'
3232
})
3333
})
34-
test('should allow rehydration of single class value', () => {
34+
test.only('should allow rehydration of single class value', () => {
3535
expect.assertions(1)
3636
class User {
3737
name = 'Bob'
@@ -51,7 +51,7 @@ describe('REHYDRATE', () => {
5151
}
5252
const state: State = {
5353
user: null,
54-
user2: new User()
54+
user2: new User(),
5555
}
5656
rehydrate(state, {
5757
user: {
@@ -62,7 +62,7 @@ describe('REHYDRATE', () => {
6262
}
6363
}, {
6464
user: User.fromJSON,
65-
user2: User.fromJSON
65+
user2: User.fromJSON,
6666
})
6767

6868
expect(state).toEqual({

packages/node_modules/overmind/src/rehydrate.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { IMutation } from 'proxy-state-tree'
22

3+
import { SubType } from './internalTypes'
4+
35
export function rehydrateState(target: any, source: any, classes: any = {}) {
46
if (!target || !source) {
57
throw new Error(`You have to pass a "target" and "source" object to rehydrate`)
@@ -40,18 +42,24 @@ export type Serializable = Serialize | {
4042
}
4143
}
4244

43-
type StateNode = { [key: string]: any }
45+
export type StateNode = { [key: string]: any }
46+
47+
export type SerializableValue = Serializable | Array<Serializable> | { [key: string]: Serializable }
48+
49+
export type Classes<T extends StateNode, U = SubType<T, SerializableValue | null>> = {
50+
[P in keyof U]?: (data: any) => U[P]
51+
}
52+
53+
export type RehydrateClasses<T extends StateNode, N = SubType<Omit<T, keyof Classes<T>>, StateNode>> = Classes<T> & SubType<{
54+
[P in keyof N]?: Classes<N[P]>
55+
}, { [key: string]: (data: any) => any}>
56+
57+
/*
4458
45-
export type RehydrateClasses<T extends StateNode> = Pick<{
46-
[P in keyof T]: T[P] extends Serializable ? (data: any) => T[P] :
47-
T[P] extends Array<Serializable> ? (data: any) => T[P][keyof T[P]] :
48-
T[P] extends { [key: string]: Serializable } ? (data: any) => T[P][keyof T[P]] :
49-
T[P] extends StateNode ? RehydrateClasses<T[P]> :
50-
never
51-
},{ [Key in keyof T]: T[Key] extends Serializable | Array<Serializable> | { [key: string]: Serializable } ? Key : never }[keyof T]>
59+
*/
5260

5361

54-
export const rehydrate = <T extends { [key: string]: any }>(state: T, source: IMutation[] | StateNode, classes: RehydrateClasses<T> = {} as any) => {
62+
export const rehydrate = <T extends StateNode>(state: T, source: IMutation[] | StateNode, classes: RehydrateClasses<T> = {} as any) => {
5563
if (Array.isArray(source)) {
5664
const mutations = source as IMutation[]
5765
mutations.forEach((mutation) => {

0 commit comments

Comments
 (0)