Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types)(middleware/devtools): avoid copying types #1991

Merged
merged 8 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test-old-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
fail-fast: false
matrix:
typescript:
- 5.1.6
- 5.0.4
- 4.9.5
- 4.8.4
- 4.7.4
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ Basic typescript usage doesn't require anything special except for writing `crea
```ts
import { create } from 'zustand'
import { devtools, persist } from 'zustand/middleware'
import type {} from '@redux-devtools/extension' // required for devtools typing

interface BearState {
bears: number
Expand Down
77 changes: 7 additions & 70 deletions src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,11 @@ import type {
StoreMutatorIdentifier,
} from '../vanilla.ts'

// Copy types to avoid import type { Config } from '@redux-devtools/extension'
// https://github.com/pmndrs/zustand/issues/1205
type Action<T = any> = {
type: T
}
type ActionCreator<A, P extends any[] = any[]> = {
(...args: P): A
}
type EnhancerOptions = {
name?: string
actionCreators?:
| ActionCreator<any>[]
| {
[key: string]: ActionCreator<any>
}
latency?: number
maxAge?: number
serialize?:
| boolean
| {
options?:
| undefined
| boolean
| {
date?: true
regex?: true
undefined?: true
error?: true
symbol?: true
map?: true
set?: true
function?: true | ((fn: (...args: any[]) => any) => string)
}
replacer?: (key: string, value: unknown) => any
reviver?: (key: string, value: unknown) => any
immutable?: any
refs?: any
}
actionSanitizer?: <A extends Action>(action: A, id: number) => A
stateSanitizer?: <S>(state: S, index: number) => S
actionsBlacklist?: string | string[]
actionsWhitelist?: string | string[]
actionsDenylist?: string | string[]
actionsAllowlist?: string | string[]
predicate?: <S, A extends Action>(state: S, action: A) => boolean
shouldRecordChanges?: boolean
pauseActionType?: string
autoPause?: boolean
shouldStartLocked?: boolean
shouldHotReload?: boolean
shouldCatchErrors?: boolean
features?: {
pause?: boolean
lock?: boolean
persist?: boolean
export?: boolean | 'custom'
import?: boolean | 'custom'
jump?: boolean
skip?: boolean
reorder?: boolean
dispatch?: boolean
test?: boolean
}
trace?: boolean | (<A extends Action>(action: A) => string)
traceLimit?: number
}
type Config = EnhancerOptions & {
type?: string
}
type Config = Parameters<
(Window extends { __REDUX_DEVTOOLS_EXTENSION__?: infer T }
? T
: any)['connect']
>[0]

declare module '../vanilla' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -124,6 +60,7 @@ type StoreDevtools<S> = S extends {
: never

export interface DevtoolsOptions extends Config {
name?: string
enabled?: boolean
anonymousActionType?: string
store?: string
Expand Down Expand Up @@ -236,7 +173,7 @@ const devtoolsImpl: DevtoolsImpl =
;(api.setState as NamedSet<S>) = (state, replace, nameOrAction) => {
const r = set(state, replace)
if (!isRecording) return r
const action: Action<unknown> =
const action: { type: unknown } =
nameOrAction === undefined
? { type: anonymousActionType || 'anonymous' }
: typeof nameOrAction === 'string'
Expand Down
Loading