-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document the MyTypes global namespace pattern #306
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -135,6 +135,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre | |||||
| - [Connect with `react-redux`](#connect-with-react-redux) | ||||||
| - [Typing connected component](#typing-connected-component) | ||||||
| - [Typing `useSelector` and `useDispatch`](#typing-useselector-and-usedispatch) | ||||||
| - [Types global namespace](#types-global-namespace) | ||||||
| - [Typing connected component with `redux-thunk` integration](#typing-connected-component-with-redux-thunk-integration) | ||||||
| - [Configuration & Dev Tools](#configuration--dev-tools) | ||||||
| - [Common Npm Scripts](#common-npm-scripts) | ||||||
|
|
@@ -1837,6 +1838,56 @@ export const useDispatch: () => Dispatch<RootAction> = useGenericDispatch; | |||||
|
|
||||||
| [⇧ back to top](#table-of-contents) | ||||||
|
|
||||||
| ### Types global namespace | ||||||
|
|
||||||
| The `MyTypes` module is a shared namespace that lets each feature own its local | ||||||
| types while still contributing to a central app-level contract. It builds on the | ||||||
| `RootState` and `RootAction` setup from the [Create Global Store | ||||||
| Types](#create-global-store-types) section above. | ||||||
|
|
||||||
| - `playground/typings/modules.d.ts` provides the empty ambient module that later | ||||||
| files augment. | ||||||
| - `playground/src/store/types.d.ts` augments it with `Store`, `RootAction`, and | ||||||
| `RootState`. | ||||||
| - `playground/src/services/types.d.ts` augments the same module with `Services`. | ||||||
|
|
||||||
| Because each module augments `MyTypes` independently, types stay close to the | ||||||
| code that owns them instead of being collected in one giant file. | ||||||
|
|
||||||
| ```tsx | ||||||
| import { StateType, ActionType } from 'typesafe-actions'; | ||||||
|
|
||||||
| declare module 'MyTypes' { | ||||||
| export type Store = StateType<typeof import('./store').default>; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| export type RootAction = ActionType<typeof import('./root-action').default>; | ||||||
| export type RootState = StateType<ReturnType<typeof import('./root-reducer').default>>; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| declare module 'typesafe-actions' { | ||||||
| interface Types { | ||||||
| RootAction: ActionType<typeof import('./root-action').default>; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| ```tsx | ||||||
| declare module 'MyTypes' { | ||||||
| export type Services = typeof import('./index').default; | ||||||
| } | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| ```ts | ||||||
| import { RootState, RootAction, Services } from 'MyTypes'; | ||||||
| ``` | ||||||
|
|
||||||
| This pattern keeps imports stable and makes cross-cutting app types reusable in | ||||||
| selectors, store hooks, middleware, and connected components without duplicating | ||||||
| definitions. | ||||||
|
|
||||||
| [⇧ back to top](#table-of-contents) | ||||||
|
|
||||||
| ### Typing connected component with `redux-thunk` integration | ||||||
|
|
||||||
| _**NOTE**: When using thunk action creators you need to use `bindActionCreators`. Only this way you can get corrected dispatch props type signature like below.*_ | ||||||
|
|
@@ -2091,7 +2142,7 @@ if you cannot find types for a third-party module you can provide your own types | |||||
|
|
||||||
| ```tsx | ||||||
| // typings/modules.d.ts | ||||||
| declare module 'MyTypes'; | ||||||
| declare module 'MyTypes' {} | ||||||
| declare module 'react-test-renderer'; | ||||||
| declare module '@storybook/addon-storyshots' | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre | |
| - [Connect with `react-redux`](#connect-with-react-redux) | ||
| - [Typing connected component](#typing-connected-component) | ||
| - [Typing `useSelector` and `useDispatch`](#typing-useselector-and-usedispatch) | ||
| - [Types global namespace](#types-global-namespace) | ||
| - [Typing connected component with `redux-thunk` integration](#typing-connected-component-with-redux-thunk-integration) | ||
| - [Configuration & Dev Tools](#configuration--dev-tools) | ||
| - [Common Npm Scripts](#common-npm-scripts) | ||
|
|
@@ -718,6 +719,36 @@ const mapDispatchToProps = (dispatch: Dispatch<MyTypes.RootAction>) => | |
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### Types global namespace | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| The `MyTypes` module is a shared namespace that lets each feature own its local | ||
| types while still contributing to a central app-level contract. It builds on the | ||
| `RootState` and `RootAction` setup from the [Create Global Store | ||
| Types](#create-global-store-types) section above. | ||
|
|
||
| - `playground/typings/modules.d.ts` provides the empty ambient module that later | ||
| files augment. | ||
| - `playground/src/store/types.d.ts` augments it with `Store`, `RootAction`, and | ||
| `RootState`. | ||
| - `playground/src/services/types.d.ts` augments the same module with `Services`. | ||
|
|
||
| Because each module augments `MyTypes` independently, types stay close to the | ||
| code that owns them instead of being collected in one giant file. | ||
|
|
||
| ::codeblock='playground/src/store/types.d.ts':: | ||
|
|
||
| ::codeblock='playground/src/services/types.d.ts':: | ||
|
|
||
| ```ts | ||
| import { RootState, RootAction, Services } from 'MyTypes'; | ||
| ``` | ||
|
|
||
| This pattern keeps imports stable and makes cross-cutting app types reusable in | ||
| selectors, store hooks, middleware, and connected components without duplicating | ||
| definitions. | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### Typing connected component with `redux-thunk` integration | ||
|
|
||
| _**NOTE**: When using thunk action creators you need to use `bindActionCreators`. Only this way you can get corrected dispatch props type signature like below.*_ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // typings/modules.d.ts | ||
| declare module 'MyTypes'; | ||
| declare module 'MyTypes' {} | ||
| declare module 'react-test-renderer'; | ||
| declare module '@storybook/addon-storyshots' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new section "Types global namespace" significantly overlaps with the existing "Create Global Store Types" section (around line 1288). To improve maintainability and avoid documentation drift, consider consolidating these sections or having one reference the other.