Skip to content

Commit 884c49e

Browse files
committed
fix types
1 parent 4c1cafa commit 884c49e

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
expect,
3+
jest,
4+
test,
5+
describe,
6+
beforeEach,
7+
afterEach,
8+
} from '@jest/globals';
9+
import { createKey } from '../slice/key';
10+
11+
const sliceAKey = createKey('sliceA', []);
12+
13+
const widescreenField = sliceAKey.field(false);
14+
15+
const sliceA = sliceAKey.slice({
16+
widescreen: widescreenField,
17+
});
18+
19+
const sliceUIKey = createKey('slice-ui', [sliceA]);
20+
21+
const widescreenFieldExported = sliceUIKey.derive((state) => {
22+
return sliceA.getField(state, 'widescreen');
23+
});
24+
25+
const sliceUI = sliceUIKey.slice({
26+
widescreen: widescreenFieldExported,
27+
});
28+
29+
test('should allow this as sliceUI is a dependency of sliceB', () => {
30+
const sliceBKey = createKey('sliceB', [sliceUI]);
31+
32+
const sliceB = sliceBKey.slice({});
33+
sliceBKey.effect((store) => {
34+
// should allow this as sliceUI is a dependency of sliceB
35+
const { widescreen } = sliceUI.track(store);
36+
sliceUI.get(store.state);
37+
38+
sliceUI.getField(store.state, 'widescreen');
39+
});
40+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
expect,
3+
jest,
4+
test,
5+
describe,
6+
beforeEach,
7+
afterEach,
8+
} from '@jest/globals';
9+
import { createKey } from '../slice/key';
10+
11+
const sliceAKey = createKey('sliceA', []);
12+
13+
const widescreenField = sliceAKey.field(false);
14+
15+
const sliceA = sliceAKey.slice({
16+
widescreen: widescreenField,
17+
});
18+
19+
const sliceUIKey = createKey('slice-ui', [sliceA]);
20+
21+
const widescreenFieldExported = sliceUIKey.derive((state) => {
22+
return sliceA.getField(state, 'widescreen');
23+
});
24+
25+
const sliceUI = sliceUIKey.slice({
26+
widescreen: widescreenFieldExported,
27+
});
28+
29+
test('should error as sliceUI is not a dependency of sliceB', () => {
30+
const sliceBKey = createKey('sliceB', []);
31+
32+
const sliceB = sliceBKey.slice({});
33+
sliceBKey.effect((store) => {
34+
// @ts-expect-error - should error as sliceUI is not a dependency of sliceB
35+
const { widescreen } = sliceUI.track(store);
36+
// @ts-expect-error - should error as sliceUI is not a dependency of sliceB
37+
sliceUI.get(store.state);
38+
// @ts-expect-error - should error as sliceUI is not a dependency of sliceB
39+
sliceUI.getField(store.state, 'widescreen');
40+
});
41+
});

packages/core/src/slice/field.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export abstract class BaseField<
2727
}
2828

2929
abstract get<TState extends StoreState<any>>(
30-
storeState: IfSubsetOfState<TName | TDep, TState>,
30+
storeState: IfSubsetOfState<TName, TState>,
3131
): TVal;
3232

3333
isEqual(a: TVal, b: TVal): boolean {
@@ -73,7 +73,7 @@ export class DerivedField<
7373
private prevValCache = new WeakMap<StoreState<any>['_ref'], TVal>();
7474

7575
get<TState extends StoreState<any>>(
76-
storeState: IfSubsetOfState<TName | TDep, TState>,
76+
storeState: IfSubsetOfState<TName, TState>,
7777
): TVal {
7878
if (!this.id) {
7979
throwValidationError(
@@ -130,7 +130,7 @@ export class StateField<
130130
}
131131

132132
get<TState extends StoreState<any>>(
133-
storeState: IfSubsetOfState<TName | TDep, TState>,
133+
storeState: IfSubsetOfState<TName, TState>,
134134
): TVal {
135135
if (!this.id) {
136136
throwValidationError(

packages/core/src/slice/slice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Slice<
104104
}
105105

106106
get<TState extends StoreState<any>>(
107-
storeState: IfSubsetOfState<TName | TDep, TState>,
107+
storeState: IfSubsetOfState<TName, TState>,
108108
): Simplify<InferSliceFieldState<TExternal>> {
109109
const existing = this.getCache.get(storeState);
110110

@@ -171,14 +171,14 @@ export class Slice<
171171
T extends keyof InferSliceFieldState<TExternal>,
172172
TState extends StoreState<any>,
173173
>(
174-
storeState: IfSubsetOfState<TName | TDep, TState>,
174+
storeState: IfSubsetOfState<TName, TState>,
175175
fieldName: T,
176176
): InferSliceFieldState<TExternal>[T] {
177177
return this._getFieldByName(fieldName as string).get(storeState) as any;
178178
}
179179

180180
track<TEStore extends EffectStore>(
181-
store: IfSubsetEffectStore<TName | TDep, TEStore>,
181+
store: IfSubsetEffectStore<TName, TEStore>,
182182
): Simplify<InferSliceFieldState<TExternal>> {
183183
return new Proxy(this.get(store.state), {
184184
get: (target, prop: string, receiver) => {

0 commit comments

Comments
 (0)