Skip to content

Commit

Permalink
chore: fix issues with Solid store tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 30, 2024
1 parent 6e5156d commit 2baec1f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/angular-store/tests/test.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expectTypeOf, test } from 'vitest'
import { Derived, Store, injectStore } from '../src'
import type { Signal } from '@angular/core'

test('useStore works with derived state', () => {
test('injectStore works with derived state', () => {
const store = new Store(12)
const derived = new Derived({
deps: [store],
Expand Down
18 changes: 11 additions & 7 deletions packages/solid-store/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { onCleanup } from 'solid-js'
import { createStore, reconcile } from 'solid-js/store'
import type { AnyUpdater, Store } from '@tanstack/store'
import type { Derived, Store } from '@tanstack/store'
import type { Accessor } from 'solid-js'

export * from '@tanstack/store'
Expand All @@ -10,12 +10,16 @@ export * from '@tanstack/store'
*/
export type NoInfer<T> = [T][T extends any ? 0 : never]

export function useStore<
TState,
TSelected = NoInfer<TState>,
TUpdater extends AnyUpdater = AnyUpdater,
>(
store: Store<TState, TUpdater>,
export function useStore<TState, TSelected = NoInfer<TState>>(
store: Store<TState, any>,
selector?: (state: NoInfer<TState>) => TSelected,
): Accessor<TSelected>
export function useStore<TState, TSelected = NoInfer<TState>>(
store: Derived<TState, any>,
selector?: (state: NoInfer<TState>) => TSelected,
): Accessor<TSelected>
export function useStore<TState, TSelected = NoInfer<TState>>(
store: Store<TState, any> | Derived<TState, any>,
selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,
): Accessor<TSelected> {
const [slice, setSlice] = createStore({
Expand Down
20 changes: 20 additions & 0 deletions packages/solid-store/tests/test.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectTypeOf, test } from 'vitest'
import { Derived, Store, useStore } from '../src'
import type { Accessor } from 'solid-js'

test('useStore works with derived state', () => {
const store = new Store(12)
const derived = new Derived({
deps: [store],
fn: () => {
return { val: store.state * 2 }
},
})

const val = useStore(derived, (state) => {
expectTypeOf(state).toMatchTypeOf<{ val: number }>()
return state.val
})

expectTypeOf(val).toMatchTypeOf<Accessor<number>>()
})

0 comments on commit 2baec1f

Please sign in to comment.