fix(middleware): devtools type error #2700 (#2875)

* fix: add an inferred generic U to CreateStoreImpl

* fix: add inferred generic U variables to both Devtools and DevtoolsImpl

* test: add tests for issue #2700

* revert impl types

* code styling

---------

Co-authored-by: daishi <daishi@axlight.com>
This commit is contained in:
Andy Zhou 2024-12-04 22:00:01 +09:00 committed by GitHub
parent 5ed67ec1a9
commit af7ff21190
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -79,8 +79,9 @@ type Devtools = <
T,
Mps extends [StoreMutatorIdentifier, unknown][] = [],
Mcs extends [StoreMutatorIdentifier, unknown][] = [],
U = T,
>(
initializer: StateCreator<T, [...Mps, ['zustand/devtools', never]], Mcs>,
initializer: StateCreator<T, [...Mps, ['zustand/devtools', never]], Mcs, U>,
devtoolsOptions?: DevtoolsOptions,
) => StateCreator<T, Mps, [['zustand/devtools', never], ...Mcs]>

View File

@ -177,6 +177,24 @@ describe('counter state spec (single middleware)', () => {
expect(testSubtyping).toBeDefined()
})
it('devtools #2700', () => {
type TableStore = {
table: string
}
const useStoreA = create<TableStore | null>()(
devtools((_set) => null, { name: 'table-storage' }),
)
expect(useStoreA).toBeDefined()
const useStoreB = create<TableStore | null>()(
devtools(() => null, { name: 'table-storage' }),
)
expect(useStoreB).toBeDefined()
const useStoreC = create<TableStore | null>()((_set) => null)
expect(useStoreC).toBeDefined()
const useStoreD = create<TableStore | null>()(() => null)
expect(useStoreD).toBeDefined()
})
it('subscribeWithSelector', () => {
const useBoundStore = create<CounterState>()(
subscribeWithSelector((set, get) => ({