From c5d53353c79896544cfea9a6ea362088024b52d0 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Sun, 17 Apr 2022 20:31:37 +0200 Subject: [PATCH] drop pre-TS-4.1 types and workarounds (#2249) * drop pre-4.1 type compatibility hack * update test matrix, remove pre41 annotations --- .github/workflows/tests.yml | 6 +----- packages/toolkit/scripts/build.ts | 7 ------- packages/toolkit/src/query/react/module.ts | 2 +- .../{versionedTypes/ts41Types.ts => namedHooks.ts} | 4 +--- .../toolkit/src/query/react/versionedTypes/index.ts | 8 -------- .../src/query/react/versionedTypes/package.dist.json | 10 ---------- .../src/query/react/versionedTypes/ts40Types.ts | 9 --------- packages/toolkit/src/query/tests/buildHooks.test.tsx | 4 ---- packages/toolkit/src/query/tests/unionTypes.test.ts | 6 ------ 9 files changed, 3 insertions(+), 53 deletions(-) rename packages/toolkit/src/query/react/{versionedTypes/ts41Types.ts => namedHooks.ts} (84%) delete mode 100644 packages/toolkit/src/query/react/versionedTypes/index.ts delete mode 100644 packages/toolkit/src/query/react/versionedTypes/package.dist.json delete mode 100644 packages/toolkit/src/query/react/versionedTypes/ts40Types.ts diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8586ae1757..a29a1f51af 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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.1-rc', 'next'] + ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7.0-beta', 'next'] steps: - name: Checkout repo uses: actions/checkout@v2 @@ -123,10 +123,6 @@ 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 diff --git a/packages/toolkit/scripts/build.ts b/packages/toolkit/scripts/build.ts index d36a1d1cc0..c77391d79a 100644 --- a/packages/toolkit/scripts/build.ts +++ b/packages/toolkit/scripts/build.ts @@ -326,13 +326,6 @@ 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 { diff --git a/packages/toolkit/src/query/react/module.ts b/packages/toolkit/src/query/react/module.ts index 538ecdbd94..5028c64fe7 100644 --- a/packages/toolkit/src/query/react/module.ts +++ b/packages/toolkit/src/query/react/module.ts @@ -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 './versionedTypes' +import type { HooksWithUniqueNames } from './namedHooks' import { useDispatch as rrUseDispatch, diff --git a/packages/toolkit/src/query/react/versionedTypes/ts41Types.ts b/packages/toolkit/src/query/react/namedHooks.ts similarity index 84% rename from packages/toolkit/src/query/react/versionedTypes/ts41Types.ts rename to packages/toolkit/src/query/react/namedHooks.ts index 9c5dff3f14..5c93404459 100644 --- a/packages/toolkit/src/query/react/versionedTypes/ts41Types.ts +++ b/packages/toolkit/src/query/react/namedHooks.ts @@ -1,4 +1,4 @@ -import type { UseMutation, UseLazyQuery, UseQuery } from '../buildHooks' +import type { UseMutation, UseLazyQuery, UseQuery } from './buildHooks' import type { DefinitionType, EndpointDefinitions, @@ -6,8 +6,6 @@ import type { 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 = keyof Definitions extends infer Keys ? Keys extends string diff --git a/packages/toolkit/src/query/react/versionedTypes/index.ts b/packages/toolkit/src/query/react/versionedTypes/index.ts deleted file mode 100644 index 279cdceafb..0000000000 --- a/packages/toolkit/src/query/react/versionedTypes/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -// 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' diff --git a/packages/toolkit/src/query/react/versionedTypes/package.dist.json b/packages/toolkit/src/query/react/versionedTypes/package.dist.json deleted file mode 100644 index 3e11bf7b00..0000000000 --- a/packages/toolkit/src/query/react/versionedTypes/package.dist.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "typesVersions": { - ">=4.1": { - "index": ["./ts41Types.d.ts"] - }, - "<4.1": { - "index": ["./ts40Types.d.ts"] - } - } -} diff --git a/packages/toolkit/src/query/react/versionedTypes/ts40Types.ts b/packages/toolkit/src/query/react/versionedTypes/ts40Types.ts deleted file mode 100644 index 698371ad53..0000000000 --- a/packages/toolkit/src/query/react/versionedTypes/ts40Types.ts +++ /dev/null @@ -1,9 +0,0 @@ -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 {} diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index 0adf60eb10..e374b32a90 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -1828,14 +1828,10 @@ 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 () => { diff --git a/packages/toolkit/src/query/tests/unionTypes.test.ts b/packages/toolkit/src/query/tests/unionTypes.test.ts index 1676df30ce..a5ef34fecd 100644 --- a/packages/toolkit/src/query/tests/unionTypes.test.ts +++ b/packages/toolkit/src/query/tests/unionTypes.test.ts @@ -141,7 +141,6 @@ describe.skip('TS only tests', () => { expectType(result) } }) - // pre41-remove-start test('useQuery TS4.1 union', () => { const result = api.useTestQuery() @@ -207,7 +206,6 @@ describe.skip('TS only tests', () => { expectType(result) } }) - // pre41-remove-end test('useLazyQuery union', () => { const [_trigger, result] = api.endpoints.test.useLazyQuery() @@ -275,7 +273,6 @@ describe.skip('TS only tests', () => { } }) - // pre41-remove-start test('useLazyQuery TS4.1 union', () => { const [_trigger, result] = api.useLazyTestQuery() @@ -341,7 +338,6 @@ describe.skip('TS only tests', () => { expectType(result) } }) - // pre41-remove-end test('queryHookResult (without selector) union', () => { const useQueryStateResult = api.endpoints.test.useQueryState() @@ -501,7 +497,6 @@ describe.skip('TS only tests', () => { })(result) }) - // pre41-remove-start test('useMutation TS4.1 union', () => { const [_trigger, result] = api.useMutationMutation() @@ -552,5 +547,4 @@ describe.skip('TS only tests', () => { expectType(result) } }) - // pre41-remove-end })