Skip to content

Commit

Permalink
refactor: prefer interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jun 16, 2022
1 parent 2b698fb commit 40bf17b
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
"jest/consistent-test-it": [
"error",
{ "fn": "it", "withinDescribe": "it" }
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useStore,
} from 'zustand'

type UseContextStore<S extends StoreApi<State>> = {
interface UseContextStore<S extends StoreApi<State>> {
(): ExtractState<S>
<U>(
selector: StateSelector<ExtractState<S>, U>,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare module '../vanilla' {
}

// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
type Message = {
interface Message {
type: string
payload?: any
state?: any
Expand Down
13 changes: 8 additions & 5 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import {
StoreMutatorIdentifier,
} from '../vanilla'

export type StateStorage = {
export interface StateStorage {
getItem: (name: string) => string | null | Promise<string | null>
setItem: (name: string, value: string) => void | Promise<void>
removeItem: (name: string) => void | Promise<void>
}

type StorageValue<S> = { state: S; version?: number }
interface StorageValue<S> {
state: S
version?: number
}

export type PersistOptions<S, PersistedState = S> = {
export interface PersistOptions<S, PersistedState = S> {
/** Name of the storage (must be unique) */
name: string
/**
Expand Down Expand Up @@ -74,7 +77,7 @@ export type PersistOptions<S, PersistedState = S> = {

type PersistListener<S> = (state: S) => void

type StorePersist<S extends State, Ps> = {
interface StorePersist<S extends State, Ps> {
persist: {
setOptions: (options: Partial<PersistOptions<S, Ps>>) => void
clearStorage: () => void
Expand All @@ -85,7 +88,7 @@ type StorePersist<S extends State, Ps> = {
}
}

type Thenable<Value> = {
interface Thenable<Value> {
then<V>(
onFulfilled: (value: Value) => V | Promise<V> | Thenable<V>
): Thenable<V>
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { NamedSet } from './devtools'
type Write<T extends object, U extends object> = Omit<T, keyof U> & U
type Cast<T, U> = T extends U ? T : U

type Action = {
interface Action {
type: unknown
}

type ReduxState<A extends Action> = {
interface ReduxState<A extends Action> {
dispatch: StoreRedux<A>['dispatch']
}

type StoreRedux<A extends Action> = {
interface StoreRedux<A extends Action> {
dispatch: (a: A) => A
dispatchFromDevtools: true
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/subscribeWithSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare module '../vanilla' {
}
}

type StoreSubscribeWithSelector<T extends State> = {
interface StoreSubscribeWithSelector<T extends State> {
subscribe: {
(listener: (selectedState: T, previousSelectedState: T) => void): () => void
<U>(
Expand Down
2 changes: 1 addition & 1 deletion src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type UseBoundStore<S extends WithReact<StoreApi<State>>> = {
): U
} & S

type Create = {
interface Create {
<T extends State, Mos extends [StoreMutatorIdentifier, unknown][] = []>(
initializer: StateCreator<T, [], Mos>
): UseBoundStore<Mutate<StoreApi<T>, Mos>>
Expand Down
8 changes: 4 additions & 4 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type StateListener<T> = (state: T, previousState: T) => void
* @deprecated Use `StateListener<T>` instead of `StateSliceListener<T>`.
*/
export type StateSliceListener<T> = (slice: T, previousSlice: T) => void
export type Subscribe<T extends State> = {
export interface Subscribe<T extends State> {
(listener: StateListener<T>): () => void
}

Expand All @@ -21,7 +21,7 @@ export type SetState<T extends State> = {
}['_']
export type GetState<T extends State> = () => T
export type Destroy = () => void
export type StoreApi<T extends State> = {
export interface StoreApi<T extends State> {
setState: SetState<T>
getState: GetState<T>
subscribe: Subscribe<T>
Expand All @@ -40,7 +40,7 @@ export type StateCreator<
$$storeMutations: Mis
) => U) & { $$storeMutators?: Mos }

// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-interface
export interface StoreMutators<S, A> {}
export type StoreMutatorIdentifier = keyof StoreMutators<unknown, unknown>

Expand All @@ -52,7 +52,7 @@ export type Mutate<S, Ms> = Ms extends []

type Get<T, K, F = never> = K extends keyof T ? T[K] : F

type CreateStore = {
interface CreateStore {
<T extends State, Mos extends [StoreMutatorIdentifier, unknown][] = []>(
initializer: StateCreator<T, [], Mos>
): Mutate<StoreApi<T>, Mos>
Expand Down
48 changes: 34 additions & 14 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ it('creates a store hook and api object', () => {
`)
})

type CounterState = {
interface CounterState {
count: number
inc: () => void
}
Expand Down Expand Up @@ -185,8 +185,13 @@ it('can batch updates', async () => {
})

it('can update the selector', async () => {
type State = { one: string; two: string }
type Props = { selector: StateSelector<State, string> }
interface State {
one: string
two: string
}
interface Props {
selector: StateSelector<State, string>
}
const useStore = create<State>(() => ({
one: 'one',
two: 'two',
Expand All @@ -204,8 +209,12 @@ it('can update the selector', async () => {
})

it('can update the equality checker', async () => {
type State = { value: number }
type Props = { equalityFn: EqualityChecker<State> }
interface State {
value: number
}
interface Props {
equalityFn: EqualityChecker<State>
}
const useStore = create<State>(() => ({ value: 0 }))
const { setState } = useStore
const selector: StateSelector<State, State> = (s) => s
Expand Down Expand Up @@ -238,8 +247,10 @@ it('can update the equality checker', async () => {
})

it('can call useStore with progressively more arguments', async () => {
type State = { value: number }
type Props = {
interface State {
value: number
}
interface Props {
selector?: StateSelector<State, number>
equalityFn?: EqualityChecker<number>
}
Expand Down Expand Up @@ -283,7 +294,9 @@ it('can call useStore with progressively more arguments', async () => {

it('can throw an error in selector', async () => {
console.error = jest.fn()
type State = { value: string | number }
interface State {
value: string | number
}

const initialState: State = { value: 'foo' }
const useStore = create<State>(() => initialState)
Expand Down Expand Up @@ -328,7 +341,9 @@ it('can throw an error in selector', async () => {

it('can throw an error in equality checker', async () => {
console.error = jest.fn()
type State = { value: string | number }
interface State {
value: string | number
}

const initialState: State = { value: 'foo' }
const useStore = create(() => initialState)
Expand Down Expand Up @@ -373,7 +388,7 @@ it('can throw an error in equality checker', async () => {
})

it('can get the store', () => {
type State = {
interface State {
value: number
getState1: () => State
getState2: () => State
Expand All @@ -389,7 +404,7 @@ it('can get the store', () => {
})

it('can set the store', () => {
type State = {
interface State {
value: number
setState1: SetState<State>
setState2: SetState<State>
Expand Down Expand Up @@ -438,7 +453,10 @@ it('can destroy the store', () => {
})

it('only calls selectors when necessary', async () => {
type State = { a: number; b: number }
interface State {
a: number
b: number
}
const useStore = create<State>(() => ({ a: 0, b: 0 }))
const { setState } = useStore
let inlineSelectorCallCount = 0
Expand Down Expand Up @@ -474,10 +492,12 @@ it('only calls selectors when necessary', async () => {
})

it('ensures parent components subscribe before children', async () => {
type State = {
interface State {
children: { [key: string]: { text: string } }
}
type Props = { id: string }
interface Props {
id: string
}
const useStore = create<State>(() => ({
children: {
'1': { text: 'child 1' },
Expand Down
2 changes: 1 addition & 1 deletion tests/context.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ afterEach(() => {
console.error = consoleError
})

type CounterState = {
interface CounterState {
count: number
inc: () => void
}
Expand Down
6 changes: 3 additions & 3 deletions tests/middlewareTypes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { immer } from 'zustand/middleware/immer'
import createVanilla from 'zustand/vanilla'

type CounterState = {
interface CounterState {
count: number
inc: () => void
}
Expand Down Expand Up @@ -597,7 +597,7 @@ describe('more complex state spec with subscribeWithSelector', () => {
})

it('#631', () => {
type MyState = {
interface MyState {
foo: number | null
}
const useStore = create<MyState>()(
Expand All @@ -622,7 +622,7 @@ describe('more complex state spec with subscribeWithSelector', () => {
})

it('#650', () => {
type MyState = {
interface MyState {
token: string | undefined
authenticated: boolean
authenticate: (username: string, password: string) => Promise<void>
Expand Down
9 changes: 7 additions & 2 deletions tests/types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ type AssertEqual<Type, Expected> = Type extends Expected
: never

it('should have correct (partial) types for setState', () => {
type Count = { count: number }
interface Count {
count: number
}

const store = create<Count>((set) => ({
count: 0,
Expand All @@ -137,7 +139,10 @@ it('should have correct (partial) types for setState', () => {
})

it('should allow for different partial keys to be returnable from setState', () => {
type State = { count: number; something: string }
interface State {
count: number
something: string
}

const store = create<State>(() => ({
count: 0,
Expand Down

0 comments on commit 40bf17b

Please sign in to comment.