Skip to content

Commit

Permalink
Revert "drop pre-TS-4.1 types and workarounds (#2249)"
Browse files Browse the repository at this point in the history
This reverts commit 48689d4.
  • Loading branch information
phryneas committed Apr 18, 2022
1 parent c1f2d33 commit d57fe00
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
fail-fast: false
matrix:
node: ['14.x']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7.0-beta', 'next']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6.1-rc', 'next']
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand All @@ -123,6 +123,10 @@ jobs:

- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.base.json ./jest.config.js ./src/tests/*.* ./src/query/tests/*.*

- name: "@ts-ignore stuff that didn't exist pre-4.1 in the tests"
if: ${{ matrix.ts < 4.1 }}
run: sed -i -e 's/@pre41-ts-ignore/@ts-ignore/' -e '/pre41-remove-start/,/pre41-remove-end/d' ./src/tests/*.* ./src/query/tests/*.ts*

- name: Test types
run: |
yarn tsc --version
Expand Down
7 changes: 7 additions & 0 deletions packages/toolkit/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
}

// We need one additional package.json file in dist to support
// versioned types for TS <4.1
fs.copyFileSync(
'src/query/react/versionedTypes/package.dist.json',
'dist/query/react/versionedTypes/package.json'
)

if (!skipExtraction) {
for (let entryPoint of entryPoints) {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/react/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { capitalize } from '../utils'
import { safeAssign } from '../tsHelpers'
import type { BaseQueryFn } from '@reduxjs/toolkit/dist/query/baseQueryTypes'

import type { HooksWithUniqueNames } from './namedHooks'
import type { HooksWithUniqueNames } from './versionedTypes'

import {
useDispatch as rrUseDispatch,
Expand Down
8 changes: 8 additions & 0 deletions packages/toolkit/src/query/react/versionedTypes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is a dummy. In actual dev, we re-export the hooks type
// here. But, when published, the package.json in this folder will
// point TS to either ts40Types.d.ts or ts41Types.d.ts, and bypass
// index.d.ts completely.
// Overall, this setup allows us to selectively override the one
// file that has any difference between 4.1 and earlier, without
// having to ship two completely duplicate copies of our typedefs.
export { HooksWithUniqueNames } from './ts41Types'
10 changes: 10 additions & 0 deletions packages/toolkit/src/query/react/versionedTypes/package.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"typesVersions": {
">=4.1": {
"index": ["./ts41Types.d.ts"]
},
"<4.1": {
"index": ["./ts40Types.d.ts"]
}
}
}
9 changes: 9 additions & 0 deletions packages/toolkit/src/query/react/versionedTypes/ts40Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { EndpointDefinitions } from '@reduxjs/toolkit/dist/query/endpointDefinitions'

// For TS 4.0 and earlier, disallow use of the per-endpoint
// hooks defined at the root of each API object, because we
// can't use the string literal types here.
export declare type HooksWithUniqueNames<
Definitions extends EndpointDefinitions
> = unknown
export {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { UseMutation, UseLazyQuery, UseQuery } from './buildHooks'
import type { UseMutation, UseLazyQuery, UseQuery } from '../buildHooks'
import type {
DefinitionType,
EndpointDefinitions,
MutationDefinition,
QueryDefinition,
} from '@reduxjs/toolkit/dist/query/endpointDefinitions'

// For TS 4.1 and later, we can use string literal types to define
// the exact names of each endpoint's exported hooks
export type HooksWithUniqueNames<Definitions extends EndpointDefinitions> =
keyof Definitions extends infer Keys
? Keys extends string
Expand Down
4 changes: 4 additions & 0 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1828,10 +1828,14 @@ describe('hooks with createApi defaults set', () => {

const storeRef = setupApiStore(api)

// @pre41-ts-ignore
expectExactType(api.useGetPostsQuery)(api.endpoints.getPosts.useQuery)
// @pre41-ts-ignore
expectExactType(api.useUpdatePostMutation)(
// @pre41-ts-ignore
api.endpoints.updatePost.useMutation
)
// @pre41-ts-ignore
expectExactType(api.useAddPostMutation)(api.endpoints.addPost.useMutation)

test('useQueryState serves a deeply memoized value and does not rerender unnecessarily', async () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/toolkit/src/query/tests/unionTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe.skip('TS only tests', () => {
expectType<never>(result)
}
})
// pre41-remove-start
test('useQuery TS4.1 union', () => {
const result = api.useTestQuery()

Expand Down Expand Up @@ -206,6 +207,7 @@ describe.skip('TS only tests', () => {
expectType<never>(result)
}
})
// pre41-remove-end

test('useLazyQuery union', () => {
const [_trigger, result] = api.endpoints.test.useLazyQuery()
Expand Down Expand Up @@ -273,6 +275,7 @@ describe.skip('TS only tests', () => {
}
})

// pre41-remove-start
test('useLazyQuery TS4.1 union', () => {
const [_trigger, result] = api.useLazyTestQuery()

Expand Down Expand Up @@ -338,6 +341,7 @@ describe.skip('TS only tests', () => {
expectType<never>(result)
}
})
// pre41-remove-end

test('queryHookResult (without selector) union', () => {
const useQueryStateResult = api.endpoints.test.useQueryState()
Expand Down Expand Up @@ -497,6 +501,7 @@ describe.skip('TS only tests', () => {
})(result)
})

// pre41-remove-start
test('useMutation TS4.1 union', () => {
const [_trigger, result] = api.useMutationMutation()

Expand Down Expand Up @@ -547,4 +552,5 @@ describe.skip('TS only tests', () => {
expectType<never>(result)
}
})
// pre41-remove-end
})

0 comments on commit d57fe00

Please sign in to comment.