Comments (1)
Fiddled around for awhile and found a way to do it!
const actions = (store: Store<AppState>) => ({
setLoading: (state: AppState, loading: boolean) => ({ loading }),
setBlah: (state: AppState, fds: number) => ({ number })
});
type FuncTypeWithoutFirstArg<T extends (...args: any) => any> = T extends (arg1: infer U, ...args: infer V) => infer Q ? (...args: V) => void : any;
type ActionsObject = { [action: string]: (...args: any[]) => any };
type BoundActions<T extends ActionsObject> = {
[P in keyof T]: FuncTypeWithoutFirstArg<T[P]>
}
type MyBoundActions = BoundActions<ReturnType<typeof actions>>;
Now in my component I can do
interface StoreProps extends BoundActions<ReturnType<typeof actions>> {
readonly count: number;
}
And calling this.props.setLoading(false)
or whatever will be properly type checked against the bound action.
The main magic is in the FuncTypeWithoutFirstArg
type, which takes a function generic and defines a type where the first argument is omitted (which is what happens to the bound actions in your connected component).
It might be nice to have these types in the library, so I might open a PR for it
EDIT: Here's an even slicker way to do it:
import Store from "redux-zero/interfaces/Store";
export type FuncTypeWithoutFirstArg<T extends (...args: any) => any> =
T extends (arg1: infer U, ...args: infer V) => infer Q ? (...args: V) => void : any;
export type ActionsObject = { [action: string]: (...args: any[]) => any };
export type Actions<T> = (store: Store<T>) => ActionsObject;
export type BindActions<State, T extends Actions<State>> = {
[P in keyof ReturnType<T>]: FuncTypeWithoutFirstArg<ReturnType<T>[P]>
}
And then in your actions file, you just do:
const actions = (store: Store<AppState>) => ({
setLoading: (state: AppState, loading: boolean) => ({ loading }),
});
export type BoundActions = BindActions<AppState, typeof actions>
export default actions;
from redux-zero.
Related Issues (20)
- redux devtools dispatch not working HOT 2
- Adding redux-devtools options parameters HOT 1
- Unit mapToProps? (pass through) HOT 1
- Release seems to be missing types/Actions.ts HOT 5
- Use via script tags? HOT 1
- react waning for <Connect> HOT 11
- Not able to use redux-zero through UMD HOT 1
- Action type doesn't support Promise as return type HOT 1
- RFC - Remove all current binding functions except for Hooks HOT 10
- Add useActions hook for react HOT 2
- Update connect/Connect to use hooks instead of classes
- How to ensure the sequence of the async actions? HOT 2
- where is getActions exported from? HOT 1
- Proper syntax for store.setState callback?
- Docs website down?
- The latest published `types/Actions.d.ts` is not same as `types/index.d.ts` . HOT 2
- value from props is not passing
- use redux-zero in typescript HOT 1
- Can't test redux-zero with JEST and TypeScript? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-zero.