-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix: TypeScript Type Portability Issues #4467
Open
aryaemami59
wants to merge
85
commits into
reduxjs:master
Choose a base branch
from
aryaemami59:fix-type-portability-issues
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix: TypeScript Type Portability Issues #4467
aryaemami59
wants to merge
85
commits into
reduxjs:master
from
aryaemami59:fix-type-portability-issues
+7,292
−1,554
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- We also convert some of the problematic types from an `interface` to a `type` alias.
- The reason why `coreModuleName` and `reactHooksModuleName` are inaccessible is because in order for them to become public `unique symbols` we need to have `export declare const coreModuleName: unique symbol` as opposed to what `rollup-plugin-dts` does which is `declare const coreModuleName: unique symbol; export { coreModuleName }`.
- It seems like `tsup` can't handle `import type { Dispatch as ReduxDispatch }`, so we'll remove them.
- This was done to avoid name collision with `TypedActionCreator` in `src/mapBuilders.ts`
- This was done to prevent name collision with `GetState` type in `src/dynamicMiddleware/types.ts`
aryaemami59
force-pushed
the
fix-type-portability-issues
branch
2 times, most recently
from
July 9, 2024 19:14
d94126d
to
28d5a07
Compare
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- This was done to resolve potential `TS4023` errors with TypeScript version 5.5.
- Move `@examples-type-portability/bundler` from `examples/type-portability` to `examples/type-portability/bundler`. This was done to make room for a `NodeNext` test.
- Added `@examples-type-portability/nodenext-cjs` to test type portability with `moduleResolution NodeNext` and TypeScript CJS syntax.
aryaemami59
force-pushed
the
fix-type-portability-issues
branch
from
July 9, 2024 19:36
fc95fd3
to
2ca1c4a
Compare
- Added `@examples-type-portability/nodenext-esm` to test type portability with `moduleResolution NodeNext` and ESM syntax.
aryaemami59
force-pushed
the
fix-type-portability-issues
branch
from
July 9, 2024 19:43
2ca1c4a
to
da50c57
Compare
Alright this one should be good to go. Let me know if this needs anything. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
The type portability issues in this repository are primarily caused by three specific TypeScript errors:
TS4023
:Exported variable 'useAppDispatch' has or is using name 'TimeResponse' from external module "/home/runner/work/redux-toolkit/redux-toolkit/examples/publish-ci/vite/src/app/services/times" but cannot be named.
interface
to atype
alias. Other times we might have to export the problematic type.TS2742
:The inferred type of 'useGetQuotesQuery' cannot be named without a reference to '../../../node_modules/@reduxjs/toolkit/dist/query/react/buildHooks'. This is likely not portable. A type annotation is necessary.
TS2527
:The inferred type of 'quotesApiSlice' references an inaccessible 'unique symbol' type. A type annotation is necessary.
This one took a while for me to figure out, It turns out in order for a
unique symbol
type to be "accessible", theunique symbol
has to be imported upon declaration, so we want something that looks like this:But
dts-rollup-plugin
gives us this instead:But TypeScript treats
declare const coreModuleName: unique symbol
andexport { coreModuleName };
as 2 differentunique symbol
s because each reference tocoreModuleName
is completely unique.Solution: While somewhat hacky, I wrote a small script that takes all exported
unique symbol
s and converts them from:To:
This PR:
Resolves:
moduleResolution
is set tonode16
#3202createApi
Types are not Portable #3568Type error: The inferred type of 'configureStore' cannot be named without a reference to '@reduxjs/toolkit/node_modules/redux'. This is likely not portable. A type annotation is necessary.
#3962UpdateDefinitions
type not exported from@reduxjs/toolkit/dist/query/endpointDefinitions
#4492Converts the following types from
interface
totype
alias:AsyncThunkSliceReducerConfig
ReducerDefinition
AsyncThunkSliceReducerDefinition
MutationTypes
MutationThunkArg
MutationCacheLifecycleApi
MutationLifecycleApi
BaseEndpointTypes
CacheLifecyclePromises
MutationBaseLifecycleApi
QueryLifecyclePromises
LifecycleApi
PromiseWithKnownReason
QueryTypes
StartQueryActionCreatorOptions
QueryThunkArg
UseQuerySubscriptionOptions
GetSelectorsOptions
EndpointDefinitionWithQueryFn
EndpointDefinitionWithQuery
Exports the following types:
StartQueryActionCreatorOptions
CombinedSliceReducer
Exports the following values:
UNINITIALIZED_VALUE
Removes relative
declare module
statements.Patches
console-testing-library
so we can run the type tests with"moduleResolution": "Bundler"
. Previously it would fail due toconsole-testing-library
not having atypes
field in it's subpath exports.Adds 3 new example workspaces:
@examples-type-portability/bundler
to test for"moduleResolution": "Bundler"
.@examples-type-portability/nodenext-cjs
to test for"moduleResolution": "NodeNext"
with TypeScript's CJS syntax and"type": "commonjs"
inpackage.json
.@examples-type-portability/nodenext-esm
to test for"moduleResolution": "NodeNext"
with ESM syntax and"type": "module"
inpackage.json
.Things to keep in mind
Although I prefer using
interfaces
, it might be worthwhile to switch totype
aliases going forward.interfaces
are more prone to type portability issues compared totype
aliases, which often dissolve into their constituents. I highly recommend usingtype
aliases for all new types unlessinterfaces
are necessary (e.g., for declaration merging).I've noticed that using an
interface
as the return type of an external function often leads to type portability issues unless theinterface
itself is also exported.