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

createSlice: correctly infer state in TS 4.8 #2547

Closed
wants to merge 6 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
27 changes: 9 additions & 18 deletions .github/workflows/test-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,18 @@ defaults:
run:
working-directory: ./packages/rtk-query-codegen-openapi

on: [push, pull_request]
on:
push:
branches:
- master
paths:
- 'packages/rtk-query-codegen-openapi/**'
pull_request:
paths:
- 'packages/rtk-query-codegen-openapi/**'

jobs:
changes:
name: Check for changes
runs-on: ubuntu-latest
outputs:
codegen: ${{ steps.filter.outputs.codegen }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
codegen:
- 'packages/rtk-query-codegen-openapi/**'

build:
needs: changes
if: ${{ needs.changes.outputs.codegen == 'true' }}

runs-on: ubuntu-latest

strategy:
Expand Down
31 changes: 12 additions & 19 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
name: CI
on: [push, pull_request]
on:
push:
branches:
- master
paths:
- 'packages/toolkit/**'
pull_request:
paths:
- 'packages/toolkit/**'

defaults:
run:
working-directory: ./packages/toolkit

jobs:
changes:
name: Check for changes
runs-on: ubuntu-latest
outputs:
toolkit: ${{ steps.filter.outputs.toolkit }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
toolkit:
- 'packages/toolkit/**'

build:
needs: changes
if: ${{ needs.changes.outputs.toolkit == 'true' }}

name: Lint, Test, Build & Pack on Node ${{ matrix.node }}

runs-on: ubuntu-latest
Expand Down Expand Up @@ -96,7 +88,8 @@ jobs:
fail-fast: false
matrix:
node: ['16.x']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', 'next']
continue-on-error: ${{ matrix.ts == 'next' }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"query"
],
"dependencies": {
"@phryneas/ts-version": "^1.0.2",
"immer": "^9.0.7",
"redux": "^4.1.2",
"redux-thunk": "^2.4.1",
Expand Down
25 changes: 15 additions & 10 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,18 @@ type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
export type ValidateSliceCaseReducers<
S,
ACR extends SliceCaseReducers<S>
> = ACR & {
[T in keyof ACR]: ACR[T] extends {
reducer(s: S, action?: infer A): any
> = ACR &
{
[T in keyof ACR]: ACR[T] extends {
reducer(s: S, action?: infer A): any
}
? {
prepare(...a: never[]): Omit<A, 'type'>
}
: import('@phryneas/ts-version').TSVersion.AtLeast<4, 8> extends true
? ACR[T]
: {}
}
? {
prepare(...a: never[]): Omit<A, 'type'>
}
: {}
}

function getType(slice: string, actionKey: string): string {
return `${slice}/${actionKey}`
Expand Down Expand Up @@ -267,8 +270,10 @@ export function createSlice<
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
if(options.initialState === undefined) {
console.error('You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`')
if (options.initialState === undefined) {
console.error(
'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`'
)
}
}

Expand Down
23 changes: 19 additions & 4 deletions packages/toolkit/src/tests/createSlice.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ const value = actionCreators.anyKey
name: 'counter',
initialState: 0,
reducers: {
increment: (state: number, action) => state + action.payload,
increment: (state: number, action) => {
// @ts-expect-error
expectType<string>(state)
return state + action.payload
},
decrement: (state: number, action) => state - action.payload,
},
extraReducers: {
Expand Down Expand Up @@ -92,14 +96,25 @@ const value = actionCreators.anyKey
initialState: 0,
reducers: {
increment: (state) => state + 1,
decrement: (state, { payload = 1 }: PayloadAction<number | undefined>) =>
state - payload,
decrement: (
state,
{ payload = 1 }: PayloadAction<number | undefined>
) => {
// @ts-expect-error
expectType<string>(state)
return state - payload
},

multiply: (state, { payload }: PayloadAction<number | number[]>) =>
Array.isArray(payload)
? payload.reduce((acc, val) => acc * val, state)
: state * payload,
addTwo: {
reducer: (s, { payload }: PayloadAction<number>) => s + payload,
reducer: (s, { payload }: PayloadAction<number>) => {
// @ts-expect-error
expect<string>(state)
return s + payload
},
prepare: (a: number, b: number) => ({
payload: a + b,
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../toolkit/tsconfig.base.json",
"extends": "./tsconfig.base.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5484,6 +5484,13 @@ __metadata:
languageName: node
linkType: hard

"@phryneas/ts-version@npm:^1.0.2":
version: 1.0.2
resolution: "@phryneas/ts-version@npm:1.0.2"
checksum: d51914a8ea35ff8b686a9379b9e9fe6d5b5feaf2e7511b880d2835015736e33bc82952bbc369918f251d4a755f32f4a9c4a34b0ec4dfdbc3e87a41d26401105c
languageName: node
linkType: hard

"@pmmmwh/react-refresh-webpack-plugin@npm:^0.5.3":
version: 0.5.7
resolution: "@pmmmwh/react-refresh-webpack-plugin@npm:0.5.7"
Expand Down Expand Up @@ -5596,6 +5603,7 @@ __metadata:
resolution: "@reduxjs/toolkit@workspace:packages/toolkit"
dependencies:
"@microsoft/api-extractor": ^7.13.2
"@phryneas/ts-version": ^1.0.2
"@size-limit/preset-small-lib": ^4.11.0
"@testing-library/react": ^13.3.0
"@testing-library/user-event": ^13.1.5
Expand Down