I would like to create a ReturnType
for all functions in a file.
For example, for redux actions file:
// files.actions.ts
export const setFiles = (files: IFile[]) => ({
type: FILES_ACTION_TYPE.SET_FILES,
payload: files,
});
export const clearFiles = () => ({
type: FILES_ACTION_TYPE.CLEAR_FILES,
payload: undefined
});
export type FilesAction = ReturnType<typeof clearFiles | typeof setFiles>
Is there an elegant way to have types of all the functions without writing each of them by hand?
While this isn't a direct answer to your question:
We would specifically recommend not writing action creators by hand. Instead, you should use the createSlice
API from our official Redux Toolkit package , as it automatically generates action creators for you. It's also already written in TS, and ensures that the action creators are correctly typed to match the reducers.
Related to that, I personally see no real benefit to attempting to define a union type of all actions that can be dispatched, or limiting the set of actions that can be passed to dispatch
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.