Skip to content

Commit

Permalink
Fix TS issues related to AnyNonNullishValue
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Sep 11, 2024
1 parent de7a9ba commit 3902ee2
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 49 deletions.
5 changes: 3 additions & 2 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isAnyOf } from './matchers'
import { nanoid } from './nanoid'
import type {
AnyNonNullishValue,
EmptyObject,
FallbackIfUnknown,
Id,
IsAny,
Expand Down Expand Up @@ -220,7 +221,7 @@ export type AsyncThunkPayloadCreatorReturnValue<
export type AsyncThunkPayloadCreator<
Returned,
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = AnyNonNullishValue,
ThunkApiConfig extends AsyncThunkConfig = EmptyObject,
> = (
arg: ThunkArg,
thunkAPI: GetThunkAPI<ThunkApiConfig>,
Expand Down Expand Up @@ -293,7 +294,7 @@ type AsyncThunkActionCreator<
*/
export type AsyncThunkOptions<
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = AnyNonNullishValue,
ThunkApiConfig extends AsyncThunkConfig = EmptyObject,
> = {
/**
* A method to control whether the asyncThunk should be executed. Has access to the
Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { executeReducerBuilderCallback } from './mapBuilders'
import type {
AnyFunction,
AnyNonNullishValue,
EmptyObject,
Id,
TypeGuard,
} from './tsHelpers'
Expand Down Expand Up @@ -304,7 +305,7 @@ type AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = AnyNonNullishValue,
ThunkApiConfig extends AsyncThunkConfig = EmptyObject,
> = {
pending?: CaseReducer<
State,
Expand All @@ -331,7 +332,7 @@ type AsyncThunkSliceReducerDefinition<
State,
ThunkArg,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = AnyNonNullishValue,
ThunkApiConfig extends AsyncThunkConfig = EmptyObject,
> = AsyncThunkSliceReducerConfig<State, ThunkArg, Returned, ThunkApiConfig> &
ReducerDefinition<ReducerType.asyncThunk> & {
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>
Expand Down Expand Up @@ -372,8 +373,7 @@ interface AsyncThunkCreator<
<
Returned,
ThunkArg,
ThunkApiConfig extends
PreventCircular<AsyncThunkConfig> = AnyNonNullishValue,
ThunkApiConfig extends PreventCircular<AsyncThunkConfig> = EmptyObject,
>(
payloadCreator: AsyncThunkPayloadCreator<
Returned,
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/devtoolsExtension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Action, ActionCreator, StoreEnhancer } from 'redux'
import { compose } from 'redux'
import type { AnyFunction, AnyNonNullishValue } from './tsHelpers'
import type { AnyFunction, EmptyObject } from './tsHelpers'

/**
* @public
Expand Down Expand Up @@ -209,7 +209,7 @@ type Compose = typeof compose

interface ComposeWithDevTools {
(options: DevToolsEnhancerOptions): Compose
<StoreExt extends AnyNonNullishValue>(
<StoreExt extends EmptyObject>(
...funcs: StoreEnhancer<StoreExt>[]
): StoreEnhancer<StoreExt>
}
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/getDefaultMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createImmutableStateInvariantMiddleware } from './immutableStateInvaria

import type { SerializableStateInvariantMiddlewareOptions } from './serializableStateInvariantMiddleware'
import { createSerializableStateInvariantMiddleware } from './serializableStateInvariantMiddleware'
import type { AnyNonNullishValue, ExcludeFromTuple } from './tsHelpers'
import type { EmptyObject, ExcludeFromTuple } from './tsHelpers'
import { Tuple } from './utils'

function isBoolean(x: any): x is boolean {
Expand All @@ -30,7 +30,7 @@ interface GetDefaultMiddlewareOptions {

export type ThunkMiddlewareFor<
S,
O extends GetDefaultMiddlewareOptions = AnyNonNullishValue,
O extends GetDefaultMiddlewareOptions = EmptyObject,
> = O extends {
thunk: false
}
Expand Down
6 changes: 3 additions & 3 deletions packages/toolkit/src/query/baseQueryTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ThunkDispatch } from '@reduxjs/toolkit'
import type { AnyNonNullishValue } from '../tsHelpers'
import type { AnyObject } from '../tsHelpers'
import type { MaybePromise, UnwrapPromise } from './tsHelpers'

export interface BaseQueryApi {
Expand Down Expand Up @@ -37,8 +37,8 @@ export type BaseQueryFn<
Args = any,
Result = unknown,
Error = unknown,
DefinitionExtraOptions = AnyNonNullishValue,
Meta = AnyNonNullishValue,
DefinitionExtraOptions = AnyObject,
Meta = AnyObject,
> = (
args: Args,
api: BaseQueryApi,
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/core/buildMiddleware/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ThunkDispatch,
UnknownAction,
} from '@reduxjs/toolkit'
import type { AnyNonNullishValue } from '../../../tsHelpers'
import type { AnyNonNullishValue, EmptyObject } from '../../../tsHelpers'
import type { Api, ApiContext } from '../../apiTypes'
import type {
AssertTagTypes,
Expand Down Expand Up @@ -66,7 +66,7 @@ export interface BuildSubMiddlewareInput
>,
queryCacheKey: string,
override?: Partial<QueryThunkArg>,
): AsyncThunkAction<ThunkResult, QueryThunkArg, AnyNonNullishValue>
): AsyncThunkAction<ThunkResult, QueryThunkArg, EmptyObject>
isThisApiSliceAction: (action: Action) => boolean
}

Expand Down
14 changes: 5 additions & 9 deletions packages/toolkit/src/query/createApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UnknownAction } from '@reduxjs/toolkit'
import { weakMapMemoize } from 'reselect'
import type { AnyNonNullishValue } from '../tsHelpers'
import type { AnyFunction, AnyObject } from '../tsHelpers'
import type { Api, ApiContext, Module, ModuleName } from './apiTypes'
import type { BaseQueryArg, BaseQueryFn } from './baseQueryTypes'
import type { CombinedState } from './core/apiState'
Expand Down Expand Up @@ -324,7 +324,9 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
endpoints,
)) {
if (typeof partialDefinition === 'function') {
partialDefinition(context.endpointDefinitions[endpointName])
;(partialDefinition as AnyFunction)(
context.endpointDefinitions[endpointName],
)
} else {
Object.assign(
context.endpointDefinitions[endpointName] || {},
Expand All @@ -335,13 +337,7 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
}
return api
},
} as Api<
BaseQueryFn,
AnyNonNullishValue,
string,
string,
Modules[number]['name']
>
} as Api<BaseQueryFn, AnyObject, string, string, Modules[number]['name']>

const initializedModules = modules.map((m) =>
m.init(api as any, optionsWithDefaults as any, context),
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/react/ApiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Context } from 'react'
import React, { useContext, useEffect } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import { Provider, ReactReduxContext } from 'react-redux'
import type { AnyNonNullishValue } from '../../tsHelpers'
import type { EmptyObject } from '../../tsHelpers'

/**
* Can be used as a `Provider` if you **do not already have a Redux store**.
Expand Down Expand Up @@ -33,7 +33,7 @@ import type { AnyNonNullishValue } from '../../tsHelpers'
*/
export function ApiProvider(props: {
children: any
api: Api<any, AnyNonNullishValue, any, any>
api: Api<any, EmptyObject, any, any>
setupListeners?: Parameters<typeof setupListeners>[1] | false
context?: Context<ReactReduxContextValue | null>
}) {
Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/tests/combineSlices.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Reducer, Slice, WithSlice } from '@reduxjs/toolkit'
import { combineSlices } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import type { AnyNonNullishValue } from '../tsHelpers'
import type { EmptyObject } from '../tsHelpers'

declare const stringSlice: Slice<string, AnyNonNullishValue, 'string'>
declare const stringSlice: Slice<string, EmptyObject, 'string'>

declare const numberSlice: Slice<number, AnyNonNullishValue, 'number'>
declare const numberSlice: Slice<number, EmptyObject, 'number'>

declare const booleanReducer: Reducer<boolean>

Expand Down Expand Up @@ -39,7 +39,7 @@ describe('type tests', () => {

expectTypeOf(
rootReducer(undefined, { type: '' }),
).toEqualTypeOf<AnyNonNullishValue>()
).toMatchTypeOf<EmptyObject>()

const declaredLazy =
combineSlices().withLazyLoadedSlices<WithSlice<typeof numberSlice>>()
Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/tests/configureStore.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { noop } from '@internal/tests/utils/helpers'
import type { AnyNonNullishValue } from '@internal/tsHelpers'
import type { AnyNonNullishValue, EmptyObject } from '@internal/tsHelpers'
import type {
Action,
ConfigureStoreOptions,
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('type tests', () => {
reducer: (): string | null => null,
})

expectTypeOf(store.getState()).toEqualTypeOf<string | null>()
expectTypeOf(store.getState()).toMatchTypeOf<string | null>()
})

test('configureStore() accepts store Tuple for enhancers, but not plain array', () => {
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('type tests', () => {
expectTypeOf(store3.anotherProperty).toBeNumber()

const someStateExtendingEnhancer: StoreEnhancer<
AnyNonNullishValue,
EmptyObject,
{ someProperty: string }
> =
(next) =>
Expand All @@ -230,7 +230,7 @@ describe('type tests', () => {
}

const anotherStateExtendingEnhancer: StoreEnhancer<
AnyNonNullishValue,
EmptyObject,
{ anotherProperty: number }
> =
(next) =>
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/tests/createAction.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
UnknownAction,
} from '@reduxjs/toolkit'
import { createAction } from '@reduxjs/toolkit'
import type { AnyNonNullishValue } from '../tsHelpers'
import type { EmptyObject } from '../tsHelpers'

describe('type tests', () => {
describe('PayloadAction', () => {
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('type tests', () => {
if (actionCreator.match(x)) {
expectTypeOf(x.type).toEqualTypeOf<'test'>()

expectTypeOf(x.payload).not.toMatchTypeOf<AnyNonNullishValue>()
expectTypeOf(x.payload).not.toMatchTypeOf<EmptyObject>()
}
})

Expand Down
18 changes: 9 additions & 9 deletions packages/toolkit/src/tests/createAsyncThunk.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { noop } from '@internal/tests/utils/helpers'
import type { AnyFunction, AnyNonNullishValue } from '@internal/tsHelpers'
import type {
AnyFunction,
AnyNonNullishValue,
EmptyObject,
} from '@internal/tsHelpers'
import type { TSVersion } from '@phryneas/ts-version'
import type {
AsyncThunk,
Expand Down Expand Up @@ -491,19 +495,15 @@ describe('type tests', () => {
return 'ret' as const
})

expectTypeOf(thunk).toEqualTypeOf<
AsyncThunk<'ret', void, AnyNonNullishValue>
>()
expectTypeOf(thunk).toEqualTypeOf<AsyncThunk<'ret', void, EmptyObject>>()
})

test('createAsyncThunk without generics, accessing `api` does not break return type', () => {
const thunk = createAsyncThunk('test', (_: void, api) => {
return 'ret' as const
})

expectTypeOf(thunk).toEqualTypeOf<
AsyncThunk<'ret', void, AnyNonNullishValue>
>()
expectTypeOf(thunk).toEqualTypeOf<AsyncThunk<'ret', void, EmptyObject>>()
})

test('createAsyncThunk rejectWithValue without generics: Expect correct return type', () => {
Expand Down Expand Up @@ -637,11 +637,11 @@ describe('type tests', () => {

test('meta return values', () => {
// return values
createAsyncThunk<'ret', void, AnyNonNullishValue>(
createAsyncThunk<'ret', void, EmptyObject>(
'test',
(_, api) => 'ret' as const,
)
createAsyncThunk<'ret', void, AnyNonNullishValue>(
createAsyncThunk<'ret', void, EmptyObject>(
'test',
async (_, api) => 'ret' as const,
)
Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/tests/createSlice.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
isRejected,
} from '@reduxjs/toolkit'
import { castDraft } from 'immer'
import type { AnyNonNullishValue } from '../tsHelpers'
import type { AnyNonNullishValue, EmptyObject } from '../tsHelpers'

describe('type tests', () => {
const counterSlice = createSlice({
Expand Down Expand Up @@ -897,20 +897,20 @@ describe('type tests', () => {
>()

expectTypeOf(slice.actions.testInferVoid).toEqualTypeOf<
AsyncThunk<void, void, AnyNonNullishValue>
AsyncThunk<void, void, EmptyObject>
>()

expectTypeOf(slice.actions.testInferVoid).toBeCallableWith()

expectTypeOf(slice.actions.testInfer).toEqualTypeOf<
AsyncThunk<TestReturned, TestArg, AnyNonNullishValue>
AsyncThunk<TestReturned, TestArg, EmptyObject>
>()

expectTypeOf(slice.actions.testExplicitType).toEqualTypeOf<
AsyncThunk<TestReturned, TestArg, { rejectValue: TestReject }>
>()

type TestInferThunk = AsyncThunk<TestReturned, TestArg, AnyNonNullishValue>
type TestInferThunk = AsyncThunk<TestReturned, TestArg, EmptyObject>

expectTypeOf(slice.caseReducers.testInfer.pending).toEqualTypeOf<
CaseReducer<TestState, ReturnType<TestInferThunk['pending']>>
Expand Down

0 comments on commit 3902ee2

Please sign in to comment.