ref: replace hardcoded types with StoreApi type (#1459)

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.
This commit is contained in:
Nurbol 2022-12-05 18:28:40 +06:00 committed by GitHub
parent ed526d074b
commit d53f92d827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 =
@ -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