Skip to content

Commit

Permalink
wip: no any arg in src
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jun 20, 2021
1 parent 28b44ff commit 9b65883
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface UseStore<T extends State> {

export default function create<
TState extends State,
CustomSetState extends SetState<any> = SetState<TState>
CustomSetState extends SetState<TState> = SetState<TState>
>(
createState: StateCreator<TState, CustomSetState> | StoreApi<TState>
): UseStore<TState> {
Expand Down
16 changes: 9 additions & 7 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export type State = object
type PartialStatePlain<
T extends State,
K1 extends keyof T = keyof T,
K2 extends keyof T = K1,
K3 extends keyof T = K2,
K4 extends keyof T = K3
> = Pick<T, K1> | Pick<T, K2> | Pick<T, K3> | Pick<T, K4> | T
type PartialStateCallable<
T extends State,
K1 extends keyof T = keyof T,
Expand All @@ -15,12 +22,8 @@ export type PartialState<
K3 extends keyof T = K2,
K4 extends keyof T = K3
> =
| PartialStatePlain<T, K1, K2, K3, K4>
| PartialStateCallable<T, K1, K2, K3, K4>
| Pick<T, K1>
| Pick<T, K2>
| Pick<T, K3>
| Pick<T, K4>
| T

export type StateSelector<T extends State, U> = (state: T) => U
export type EqualityChecker<T> = (state: T, newState: T) => boolean
Expand Down Expand Up @@ -62,7 +65,7 @@ export type StateCreator<T extends State, CustomSetState = SetState<T>> = (

export default function create<
TState extends State,
CustomSetState extends SetState<any> = SetState<TState>
CustomSetState extends SetState<TState> = SetState<TState>
>(createState: StateCreator<TState, CustomSetState>): StoreApi<TState> {
let state: TState
const listeners: Set<StateListener<TState>> = new Set()
Expand Down Expand Up @@ -117,7 +120,6 @@ export default function create<

const destroy: Destroy = () => listeners.clear()
const api = { setState, getState, subscribe, destroy }
// SetState<TState> includes CustomSetState so it should be ok
state = createState(setState as CustomSetState, getState, api)
return api
}
4 changes: 2 additions & 2 deletions tests/types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ it('should allow for different partial keys to be returnable from setState', ()
it('can accept a store has CustomSetState', () => {
function dummyMiddleware<T extends State, Settable extends keyof T>(
creator: StateCreator<T, SetState<Pick<T, Settable>>>
): StateCreator<T, SetState<Pick<T, Settable>>> {
return creator
) {
return creator as unknown as StateCreator<T, SetState<T>>
}

type ExampleState = {
Expand Down

0 comments on commit 9b65883

Please sign in to comment.