types.test.tsx
This file
Path
tests/types.test.tsx
252 lines · 6.3 KiB · TypeScript
Last changed by
chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–44why, one step back1import { expect, it } from 'vitest'2import { create } from 'zustand'3import type {4 StateCreator,5 StoreApi,6 StoreMutatorIdentifier,7 UseBoundStore,8} from 'zustand'9import { persist } from 'zustand/middleware'1011it('can use exposed types', () => {12 type ExampleState = {13 num: number14 numGet: () => number15 numGetState: () => number16 numSet: (v: number) => void17 numSetState: (v: number) => void18 }1920 const listener = (state: ExampleState) => {21 if (state) {22 const value = state.num * state.numGet() * state.numGetState()23 state.numSet(value)24 state.numSetState(value)25 }26 }27 const selector = (state: ExampleState) => state.num28 const partial: Partial<ExampleState> = {29 num: 2,30 numGet: () => 2,31 }32 const partialFn: (state: ExampleState) => Partial<ExampleState> = (33 state,34 ) => ({35 ...state,36 num: 2,37 })38 const equalityFn = (state: ExampleState, newState: ExampleState) =>39 state !== newState4041 const storeApi = create<ExampleState>((set, get) => ({42 num: 1,43 numGet: () => get().num,44 numGetState: () => {refactor: remove unused import in example & fix typo in tests (#3161)Noritaka Kobayashibefore adoption73b91d3
line 45why, one step back45 // TypeScript can't get the type of storeApi when it tries to enforce the signature of numGetState.chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 46–73why, one step back46 // Need to explicitly state the type of storeApi.getState().num or storeApi type will be type 'any'.47 const result: number = storeApi.getState().num48 return result49 },50 numSet: (v) => {51 set({ num: v })52 },53 numSetState: (v) => {54 storeApi.setState({ num: v })55 },56 }))57 const useBoundStore = storeApi5859 const stateCreator: StateCreator<ExampleState> = (set, get) => ({60 num: 1,61 numGet: () => get().num,62 numGetState: () => get().num,63 numSet: (v) => {64 set({ num: v })65 },66 numSetState: (v) => {67 set({ num: v })68 },69 })7071 function checkAllTypes(72 _getState: StoreApi<ExampleState>['getState'],73 _partialState:chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba
line 74why, one step back74 Partial<ExampleState> | ((s: ExampleState) => Partial<ExampleState>),chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 75–186why, one step back75 _setState: StoreApi<ExampleState>['setState'],76 _state: object,77 _stateListener: (state: ExampleState, previousState: ExampleState) => void,78 _stateSelector: (state: ExampleState) => number,79 _storeApi: StoreApi<ExampleState>,80 _subscribe: StoreApi<ExampleState>['subscribe'],81 _equalityFn: (a: ExampleState, b: ExampleState) => boolean,82 _stateCreator: StateCreator<ExampleState>,83 _useBoundStore: UseBoundStore<StoreApi<ExampleState>>,84 ) {85 expect(true).toBeTruthy()86 }8788 checkAllTypes(89 storeApi.getState,90 Math.random() > 0.5 ? partial : partialFn,91 storeApi.setState,92 storeApi.getState(),93 listener,94 selector,95 storeApi,96 storeApi.subscribe,97 equalityFn,98 stateCreator,99 useBoundStore,100 )101})102103type AssertEqual<Type, Expected> = Type extends Expected104 ? Expected extends Type105 ? true106 : never107 : never108109it('should have correct (partial) types for setState', () => {110 type Count = { count: number }111112 const store = create<Count>((set) => ({113 count: 0,114 // @ts-expect-error we shouldn't be able to set count to undefined115 a: () => set(() => ({ count: undefined })),116 // @ts-expect-error we shouldn't be able to set count to undefined117 b: () => set({ count: undefined }),118 c: () => set({ count: 1 }),119 }))120121 const setState: AssertEqual<122 typeof store.setState,123 StoreApi<Count>['setState']124 > = true125 expect(setState).toEqual(true)126127 // ok, should not error128 store.setState({ count: 1 })129 store.setState({})130 store.setState((previous) => previous)131132 // @ts-expect-error type undefined is not assignable to type number133 store.setState({ count: undefined })134 // @ts-expect-error type undefined is not assignable to type number135 store.setState((state) => ({ ...state, count: undefined }))136})137138it('should allow for different partial keys to be returnable from setState', () => {139 type State = {140 count: number141 something: string142 }143144 const store = create<State>(() => ({145 count: 0,146 something: 'foo',147 }))148149 const setState: AssertEqual<150 typeof store.setState,151 StoreApi<State>['setState']152 > = true153 expect(setState).toEqual(true)154155 // ok, should not error156 store.setState((previous) => {157 if (previous.count === 0) {158 return { count: 1 }159 }160 return { count: 0 }161 })162 store.setState((previous) => {163 if (previous.count === 0) {164 return { count: 1 }165 }166 if (previous.count === 1) {167 return previous168 }169 return { something: 'foo' }170 })171172 // @ts-expect-error Type '{ something: boolean; count?: undefined; }' is not assignable to type 'State'.173 store.setState((previous) => {174 if (previous.count === 0) {175 return { count: 1 }176 }177 return { something: true }178 })179})180181it('state is covariant', () => {182 const store = create<{ count: number; foo: string }>()(() => ({183 count: 0,184 foo: '',185 }))186chore(deps): update dev dependencies (#3309)Daishi Katobefore adoption350d9ec
lines 187–188why, one step back187 const testIsCovariant: StoreApi<{ count: number }> = store188 expect(testIsCovariant).toBeDefined()chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 189–190why, one step back189190 // @ts-expect-error should not compilechore(deps): update dev dependencies (#3309)Daishi Katobefore adoption350d9ec
line 191why, one step back191 const testIsNotContravariant: StoreApi<{chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 192–195why, one step back192 count: number193 foo: string194 baz: string195 }> = storechore(deps): update dev dependencies (#3309)Daishi Katobefore adoption350d9ec
line 196why, one step back196 expect(testIsNotContravariant).toBeDefined()chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 197–214why, one step back197})198199it('StateCreator<T, [StoreMutatorIdentfier, unknown][]> is StateCreator<T, []>', () => {200 interface State {201 count: number202 increment: () => void203 }204205 const foo: <M extends [StoreMutatorIdentifier, unknown][]>() => StateCreator<206 State,207 M208 > = () => (set, get) => ({209 count: 0,210 increment: () => {211 set({ count: get().count + 1 })212 },213 })214chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948
lines 215–216why, one step back215 const store = create<State>()(persist(foo(), { name: 'prefix' }))216 expect(store).toBeDefined()chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 217–233why, one step back217})218219it('StateCreator subtyping', () => {220 interface State {221 count: number222 increment: () => void223 }224225 const foo: () => StateCreator<State, []> = () => (set, get) => ({226 count: 0,227 increment: () => {228 set({ count: get().count + 1 })229 },230 })231232 create<State>()(persist(foo(), { name: 'prefix' }))233chore(deps): update dev dependencies (#3309)Daishi Katobefore adoption350d9ec
line 234why, one step back234 const testSubtyping: StateCreator<State, [['zustand/persist', unknown]]> =chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 235why, one step back235 {} as StateCreator<State, []>chore(deps): update dev dependencies (#3309)Daishi Katobefore adoption350d9ec
line 236why, one step back236 expect(testSubtyping).toBeDefined()chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 237–250why, one step back237})238239it('set state exists on store with readonly store', () => {240 interface State {241 count: number242 increment: () => void243 }244245 const useStore = create<State>()((set, get) => ({246 count: 0,247 increment: () => set({ count: get().count + 1 }),248 }))249250 useStore.setState((state) => ({ ...state, count: state.count + 1 }))chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948
line 251why, one step back251 expect(useStore).toBeDefined()chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 252why, one step back252})