Giter Site home page Giter Site logo

Writing typesafe actions about redux-zero HOT 1 CLOSED

naiyt avatar naiyt commented on June 18, 2024
Writing typesafe actions

from redux-zero.

Comments (1)

naiyt avatar naiyt commented on June 18, 2024

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

Screen Shot 2019-05-08 at 4 16 17 PM

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.