Skip to content

Commit ad77bd3

Browse files
dbritto-devCopilotdai-shi
authored
fix(devtools): improve type inference for Devtools initializer (#3511)
* fix(devtools): improve type inference for Devtools initializer * fix(devtools): enhance type inference for Devtools initializer * test(types): add devtools inference regression for #3510 * Rename options parameter to devtoolsOptions * test(types): fix lint in devtools #3510 regression case * test(types): simplify lint-safe ts-expect-error expression --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
1 parent 8476d2c commit ad77bd3

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/middleware/devtools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Devtools = <
9696
>(
9797
initializer: StateCreator<T, [...Mps, ['zustand/devtools', never]], Mcs, U>,
9898
devtoolsOptions?: DevtoolsOptions,
99-
) => StateCreator<T, Mps, [['zustand/devtools', never], ...Mcs]>
99+
) => StateCreator<T, Mps, [['zustand/devtools', never], ...Mcs], U>
100100

101101
type DevtoolsImpl = <T>(
102102
storeInitializer: StateCreator<T, [], []>,

tests/middlewareTypes.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,28 @@ describe('counter state spec (single middleware)', () => {
198198
expect(useStoreD).toBeDefined()
199199
})
200200

201+
it('devtools #3510', () => {
202+
type MyState = {
203+
count: number
204+
inc: () => void
205+
}
206+
207+
const useBoundStore = create<MyState>()(
208+
devtools((set, get) => ({
209+
count: 0,
210+
inc: () => {
211+
set({ count: get().count + 1 }, false, 'inc')
212+
// @ts-expect-error `get` should be inferred as `MyState`.
213+
void get().foo
214+
// @ts-expect-error `set` should enforce `count` as number.
215+
set({ count: '1' })
216+
},
217+
})),
218+
)
219+
220+
expect(useBoundStore).toBeDefined()
221+
})
222+
201223
it('subscribeWithSelector', () => {
202224
const useBoundStore = create<CounterState>()(
203225
subscribeWithSelector((set, get) => ({

0 commit comments

Comments
 (0)