From 40bf17bca8a82ae98430ce97ffb4a3f3fda8f5dc Mon Sep 17 00:00:00 2001 From: daishi Date: Thu, 16 Jun 2022 18:12:58 +0900 Subject: [PATCH] refactor: prefer interfaces --- .eslintrc.json | 2 +- src/context.ts | 2 +- src/middleware/devtools.ts | 2 +- src/middleware/persist.ts | 13 ++++--- src/middleware/redux.ts | 6 ++-- src/middleware/subscribeWithSelector.ts | 2 +- src/react.ts | 2 +- src/vanilla.ts | 8 ++--- tests/basic.test.tsx | 48 +++++++++++++++++-------- tests/context.test.tsx | 2 +- tests/middlewareTypes.test.tsx | 6 ++-- tests/types.test.tsx | 9 +++-- 12 files changed, 65 insertions(+), 37 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index fa14f46e4c..769cc18a0c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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" } diff --git a/src/context.ts b/src/context.ts index 44d0645c27..4d499dcba3 100644 --- a/src/context.ts +++ b/src/context.ts @@ -14,7 +14,7 @@ import { useStore, } from 'zustand' -type UseContextStore> = { +interface UseContextStore> { (): ExtractState ( selector: StateSelector, U>, diff --git a/src/middleware/devtools.ts b/src/middleware/devtools.ts index 33c69a467c..9675009b98 100644 --- a/src/middleware/devtools.ts +++ b/src/middleware/devtools.ts @@ -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 diff --git a/src/middleware/persist.ts b/src/middleware/persist.ts index 650da0a2ee..861eb0c674 100644 --- a/src/middleware/persist.ts +++ b/src/middleware/persist.ts @@ -5,15 +5,18 @@ import { StoreMutatorIdentifier, } from '../vanilla' -export type StateStorage = { +export interface StateStorage { getItem: (name: string) => string | null | Promise setItem: (name: string, value: string) => void | Promise removeItem: (name: string) => void | Promise } -type StorageValue = { state: S; version?: number } +interface StorageValue { + state: S + version?: number +} -export type PersistOptions = { +export interface PersistOptions { /** Name of the storage (must be unique) */ name: string /** @@ -74,7 +77,7 @@ export type PersistOptions = { type PersistListener = (state: S) => void -type StorePersist = { +interface StorePersist { persist: { setOptions: (options: Partial>) => void clearStorage: () => void @@ -85,7 +88,7 @@ type StorePersist = { } } -type Thenable = { +interface Thenable { then( onFulfilled: (value: Value) => V | Promise | Thenable ): Thenable diff --git a/src/middleware/redux.ts b/src/middleware/redux.ts index aece77ee0b..8bffda989e 100644 --- a/src/middleware/redux.ts +++ b/src/middleware/redux.ts @@ -4,15 +4,15 @@ import { NamedSet } from './devtools' type Write = Omit & U type Cast = T extends U ? T : U -type Action = { +interface Action { type: unknown } -type ReduxState = { +interface ReduxState { dispatch: StoreRedux['dispatch'] } -type StoreRedux = { +interface StoreRedux { dispatch: (a: A) => A dispatchFromDevtools: true } diff --git a/src/middleware/subscribeWithSelector.ts b/src/middleware/subscribeWithSelector.ts index a91e5273f6..70ba70522e 100644 --- a/src/middleware/subscribeWithSelector.ts +++ b/src/middleware/subscribeWithSelector.ts @@ -32,7 +32,7 @@ declare module '../vanilla' { } } -type StoreSubscribeWithSelector = { +interface StoreSubscribeWithSelector { subscribe: { (listener: (selectedState: T, previousSelectedState: T) => void): () => void ( diff --git a/src/react.ts b/src/react.ts index 504b82ad50..001e69f594 100644 --- a/src/react.ts +++ b/src/react.ts @@ -50,7 +50,7 @@ export type UseBoundStore>> = { ): U } & S -type Create = { +interface Create { ( initializer: StateCreator ): UseBoundStore, Mos>> diff --git a/src/vanilla.ts b/src/vanilla.ts index 4c7996fc67..f28a034111 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -9,7 +9,7 @@ export type StateListener = (state: T, previousState: T) => void * @deprecated Use `StateListener` instead of `StateSliceListener`. */ export type StateSliceListener = (slice: T, previousSlice: T) => void -export type Subscribe = { +export interface Subscribe { (listener: StateListener): () => void } @@ -21,7 +21,7 @@ export type SetState = { }['_'] export type GetState = () => T export type Destroy = () => void -export type StoreApi = { +export interface StoreApi { setState: SetState getState: GetState subscribe: Subscribe @@ -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 {} export type StoreMutatorIdentifier = keyof StoreMutators @@ -52,7 +52,7 @@ export type Mutate = Ms extends [] type Get = K extends keyof T ? T[K] : F -type CreateStore = { +interface CreateStore { ( initializer: StateCreator ): Mutate, Mos> diff --git a/tests/basic.test.tsx b/tests/basic.test.tsx index 484786c147..2290b16bad 100644 --- a/tests/basic.test.tsx +++ b/tests/basic.test.tsx @@ -37,7 +37,7 @@ it('creates a store hook and api object', () => { `) }) -type CounterState = { +interface CounterState { count: number inc: () => void } @@ -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 } + interface State { + one: string + two: string + } + interface Props { + selector: StateSelector + } const useStore = create(() => ({ one: 'one', two: 'two', @@ -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 } + interface State { + value: number + } + interface Props { + equalityFn: EqualityChecker + } const useStore = create(() => ({ value: 0 })) const { setState } = useStore const selector: StateSelector = (s) => s @@ -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 equalityFn?: EqualityChecker } @@ -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(() => initialState) @@ -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) @@ -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 @@ -389,7 +404,7 @@ it('can get the store', () => { }) it('can set the store', () => { - type State = { + interface State { value: number setState1: SetState setState2: SetState @@ -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(() => ({ a: 0, b: 0 })) const { setState } = useStore let inlineSelectorCallCount = 0 @@ -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(() => ({ children: { '1': { text: 'child 1' }, diff --git a/tests/context.test.tsx b/tests/context.test.tsx index ff2f0afbaa..bbaa2a734c 100644 --- a/tests/context.test.tsx +++ b/tests/context.test.tsx @@ -15,7 +15,7 @@ afterEach(() => { console.error = consoleError }) -type CounterState = { +interface CounterState { count: number inc: () => void } diff --git a/tests/middlewareTypes.test.tsx b/tests/middlewareTypes.test.tsx index 4ecdf08f2a..3c891e75ea 100644 --- a/tests/middlewareTypes.test.tsx +++ b/tests/middlewareTypes.test.tsx @@ -9,7 +9,7 @@ import { import { immer } from 'zustand/middleware/immer' import createVanilla from 'zustand/vanilla' -type CounterState = { +interface CounterState { count: number inc: () => void } @@ -597,7 +597,7 @@ describe('more complex state spec with subscribeWithSelector', () => { }) it('#631', () => { - type MyState = { + interface MyState { foo: number | null } const useStore = create()( @@ -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 diff --git a/tests/types.test.tsx b/tests/types.test.tsx index 69aaf230d3..7f7f6ceee8 100644 --- a/tests/types.test.tsx +++ b/tests/types.test.tsx @@ -111,7 +111,9 @@ type AssertEqual = Type extends Expected : never it('should have correct (partial) types for setState', () => { - type Count = { count: number } + interface Count { + count: number + } const store = create((set) => ({ count: 0, @@ -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(() => ({ count: 0,