Skip to content

Commit 8224baf

Browse files
committed
[#89] WIP: TS: Patch for value type deducation for a global state path
1 parent 9d45ca9 commit 8224baf

5 files changed

Lines changed: 362 additions & 2 deletions

File tree

__tests__/ts-types/GlobalState/get.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect } from 'tstyche';
1+
import { expect, test } from 'tstyche';
22

33
import GlobalState from '../../../src/GlobalState';
44
import type { ForceT } from '../../../src/utils';
@@ -46,3 +46,16 @@ expect(gs.get<ForceT, 'OK'>()).type.toEqual<'OK'>();
4646
expect(() => gs.get<ForceT, void>('some.path', {
4747
initialValue: 'invalid',
4848
})).type.toRaiseError(2322);
49+
50+
// Test for: https://github.com/birdofpreyru/react-global-state/issues/89
51+
test('type resolution with [] brackets in path', () => {
52+
type TA = Record<string, string | undefined>;
53+
type StateT = { a: Record<string, TA | undefined> };
54+
const gs2 = new GlobalState<StateT>({ a: {} });
55+
56+
const keyA: string = 'a-key';
57+
const keyB: string = 'b-key';
58+
59+
expect(gs2.get(`a.${keyA}.${keyB}`)).type.toEqual<string | undefined>();
60+
expect(gs2.get(`a['${keyA}']['${keyB}']`)).type.toEqual<string | undefined>();
61+
});

__tests__/ts-types/utils/value-at-path.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'tstyche';
1+
import type { GetFieldType } from 'lodash';
2+
import { expect, test } from 'tstyche';
23

34
import { type ValueAtPathT } from '../../../src/utils';
45

@@ -63,3 +64,21 @@ expect(y1).type.toBeNever();
6364

6465
declare const y2: ValueAtPathT<unknown, null, never>;
6566
expect(y2).type.toBeNever();
67+
68+
// Test related to: https://github.com/birdofpreyru/react-global-state/issues/89
69+
test('type resolution with [] brackets in path', () => {
70+
type TA = Record<string, string | undefined>;
71+
type StateT = { a: Record<string, TA | undefined> };
72+
73+
expect<GetFieldType<StateT, `a.${string}.${string}`>>()
74+
.type.toEqual<string | undefined>();
75+
76+
expect<GetFieldType<StateT, `a[${string}][${string}]`>>()
77+
.type.toEqual<string | undefined>();
78+
79+
expect<ValueAtPathT<StateT, `a.${string}.${string}`, never>>()
80+
.type.toEqual<string | undefined>();
81+
82+
expect<ValueAtPathT<StateT, `a[${string}][${string}]`, never>>()
83+
.type.toEqual<string | undefined>();
84+
});

0 commit comments

Comments
 (0)