Skip to content
Open
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
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1837,6 +1838,56 @@ export const useDispatch: () => Dispatch<RootAction> = useGenericDispatch;

[⇧ back to top](#table-of-contents)

### Types global namespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.


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>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The naming of the Store type alias is confusing because StateType<typeof store> returns the State type, not the Store instance type. Since RootState is already defined below, this alias might be redundant or should be renamed to reflect that it represents the state tree.

export type RootAction = ActionType<typeof import('./root-action').default>;
export type RootState = StateType<ReturnType<typeof import('./root-reducer').default>>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of ReturnType inside StateType is redundant. StateType from typesafe-actions automatically extracts the return type when a function (like a reducer) is passed.

Suggested change
export type RootState = StateType<ReturnType<typeof import('./root-reducer').default>>;
export type RootState = StateType<typeof import('./root-reducer').default>;

}

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.*_
Expand Down Expand Up @@ -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'

Expand Down
31 changes: 31 additions & 0 deletions README_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -718,6 +719,36 @@ const mapDispatchToProps = (dispatch: Dispatch<MyTypes.RootAction>) =>

[⇧ back to top](#table-of-contents)

### Types global namespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This new section overlaps with the existing "Create Global Store Types" section at line 508. Consider merging them to maintain a single source of truth for the MyTypes pattern.


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.*_
Expand Down
2 changes: 1 addition & 1 deletion playground/typings/modules.d.ts
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'