Skip to content

Strict replace types #944

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

Closed
wants to merge 4 commits into from
Closed
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
38 changes: 33 additions & 5 deletions src/middleware/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {} from '@redux-devtools/extension'
import type { Draft } from 'immer'
import {
PartialState,
SetState,
Expand Down Expand Up @@ -47,13 +48,40 @@ type TakeTwo<T> = T extends []
type WithDevtools<S> = Write<Cast<S, object>, StoreDevtools<S>>

type StoreDevtools<S> = S extends {
getState: () => infer T
setState: (...a: infer A) => infer Sr
}
? {
setState(
...a: [...a: TakeTwo<A>, actionType?: string | { type: unknown }]
): Sr
}
? A extends [
T | Partial<T> | ((state: T) => T | Partial<T>),
(boolean | undefined)?
]
? {
setState<R extends boolean | undefined>(
partial:
| (R extends true ? T : T | Partial<T>)
| ((state: T) => R extends true ? T : T | Partial<T>),
replace?: R,
action?: string | { type: unknown }
): Sr
}
: A extends [
T | Partial<T> | ((state: Draft<T>) => void),
(boolean | undefined)?
]
? {
setState<R extends boolean | undefined>(
nextStateOrUpdater:
| (R extends true ? T : T | Partial<T>)
| ((state: Draft<T>) => void),
shouldReplace?: R,
action?: string | { type: unknown }
): Sr
}
: {
setState(
...a: [...a: TakeTwo<A>, action?: string | { type: unknown }]
): Sr
}
: never

interface DevtoolsOptions {
Expand Down
20 changes: 10 additions & 10 deletions src/middleware/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ type WithImmer<S> = Write<Cast<S, object>, StoreImmer<S>>

type StoreImmer<S> = S extends {
getState: () => infer T
setState: infer SetState
setState: (...a: infer A) => infer Sr
}
? SetState extends (...a: infer A) => infer Sr
? {
setState(
nextStateOrUpdater: T | Partial<T> | ((state: Draft<T>) => void),
shouldReplace?: boolean | undefined,
...a: SkipTwo<A>
): Sr
}
: never
? {
setState<R extends boolean | undefined = false>(
nextStateOrUpdater:
| (R extends true ? T : T | Partial<T>)
| ((state: Draft<T>) => void),
shouldReplace?: R,
...a: SkipTwo<A>
): Sr
}
: never

type PopArgument<T extends (...a: never[]) => unknown> = T extends (
Expand Down
8 changes: 5 additions & 3 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export type Subscribe<T extends State> = {
}

export type SetState<T extends State> = {
_(
partial: T | Partial<T> | ((state: T) => T | Partial<T>),
replace?: boolean | undefined
_<R extends boolean | undefined = false>(
partial:
| (R extends true ? T : T | Partial<T>)
| ((state: T) => R extends true ? T : T | Partial<T>),
replace?: R
): void
}['_']
export type GetState<T extends State> = () => T
Expand Down
70 changes: 70 additions & 0 deletions tests/types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import create, {
Subscribe,
UseBoundStore,
} from 'zustand'
import { devtools } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'

it('can use exposed types', () => {
interface ExampleState {
Expand Down Expand Up @@ -188,3 +190,71 @@ it('state is covariant', () => {
baz: string
}> = store
})

it('setState with replace requires whole state', () => {
const store = create<{ count: number; something: string }>()(() => ({
count: 0,
something: 'foo',
}))

store.setState(
// @ts-expect-error missing `something`
{ count: 1 },
true
)
store.setState({ count: 1 }, false)
store.setState({ count: 1 }, true as boolean)

const storeWithDevtools = create<{ count: number; something: string }>()(
devtools(() => ({
count: 0,
something: 'foo',
}))
)

storeWithDevtools.setState(
// @ts-expect-error missing `something`
{ count: 1 },
true
)
storeWithDevtools.setState({ count: 1 }, false)
storeWithDevtools.setState({ count: 1 }, true as boolean)

storeWithDevtools.setState(
// @ts-expect-error missing `something`
{ count: 1 },
true,
'FOO'
)
storeWithDevtools.setState({ count: 1 }, false, 'FOO')
storeWithDevtools.setState({ count: 1 }, true as boolean, 'FOO')

const storeWithDevtoolsAndImmer = create<{
count: number
something: string
}>()(
immer(
devtools(() => ({
count: 0,
something: 'foo',
}))
)
)

storeWithDevtoolsAndImmer.setState(
// @ts-expect-error missing `something`
{ count: 1 },
true
)
storeWithDevtoolsAndImmer.setState({ count: 1 }, false)
storeWithDevtoolsAndImmer.setState({ count: 1 }, true as boolean)

storeWithDevtoolsAndImmer.setState(
// @ts-expect-error missing `something`
{ count: 1 },
true,
'FOO'
)
storeWithDevtoolsAndImmer.setState({ count: 1 }, false, 'FOO')
storeWithDevtoolsAndImmer.setState({ count: 1 }, true as boolean, 'FOO')
})