Skip to content

Commit

Permalink
ref: replace hardcoded types with StoreApi type
Browse files Browse the repository at this point in the history
Add binding of createStoreImpl methods types to StoreApi without hardcoding.
Makes it easier to read and reason about where the type comes from and belongs to.
Removing arrow function types reduces number of arrows, and makes it easier to read.
  • Loading branch information
Arsikod authored Dec 3, 2022
1 parent bf9d092 commit b07d80d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
let state: TState
const listeners: Set<Listener> = new Set()

const setState: SetStateInternal<TState> = (partial, replace) => {
const setState: StoreApi<TState>['setState'] = (partial, replace) => {
// TODO: Remove type assertion once https://github.com/microsoft/TypeScript/issues/37663 is resolved
// https://github.com/microsoft/TypeScript/issues/37663#issuecomment-759728342
const nextState =
Expand All @@ -77,15 +77,15 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
}
}

const getState: () => TState = () => state
const getState: StoreApi<TState>['getState'] = () => state

const subscribe: (listener: Listener) => () => void = (listener) => {
const subscribe: StoreApi<TState>['subscribe'] = (listener) => {
listeners.add(listener)
// Unsubscribe
return () => listeners.delete(listener)
}

const destroy: () => void = () => listeners.clear()
const destroy: StoreApi<TState>['destroy'] = () => listeners.clear()
const api = { setState, getState, subscribe, destroy }
state = createState(setState, getState, api)
return api as any
Expand Down

0 comments on commit b07d80d

Please sign in to comment.