Coder Social home page Coder Social logo

action-typed's People

Contributors

shakyshane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

action-typed's Issues

Return type of `createMsg`

I was looking around to see what people do for creating types for actions and reducers and found my way to your repo. I'm not actually using Redux, just React's useReducer hook, and noticed the return type on createMsg seems loose. In your examples/ts/index.ts, for example, the type of Msg("Token", "123456") is the following:

{ type: "Token"; payload: string | undefined; } | { type: "Token"; payload?: undefined; }

If the reducer is, say, function(state: /*...*/, action: Actions /* from the example */) it won't take the produced action:

Argument of type '{ type: "Token"; payload: string | undefined; } | { type: "Token"; payload?: undefined; }' is not assignable to parameter of type '{ type: "SignedIn"; payload: { firstname: string; lastname: string; }; } | { type: "Token"; payload: string; } | { type: "SignOut"; }'.
  Type '{ type: "Token"; payload: string | undefined; }' is not assignable to type '{ type: "SignedIn"; payload: { firstname: string; lastname: string; }; } | { type: "Token"; payload: string; } | { type: "SignOut"; }'.
    Type '{ type: "Token"; payload: string | undefined; }' is not assignable to type '{ type: "Token"; payload: string; }'.
      Types of property 'payload' are incompatible.
        Type 'string | undefined' is not assignable to type 'string'.
          Type 'undefined' is not assignable to type 'string'.

After much fumbling around, I did come up up with this (renamed parameters just for my personal understanding during fumble-time), though I don't know if it works for all use cases:

export function createMsg<ActionDef extends {[index: string]: any}>() {
	function create<Name extends keyof ActionDef>(type: Name, ...args: ActionDef[Name] extends undefined ? [] : [never]): {type: Name}
	function create<Name extends keyof ActionDef>(type: Name, payload: ActionDef[Name]): {type: Name, payload: ActionDef[Name]};
	function create<Name extends keyof ActionDef>(type: Name, payload?: ActionDef[Name]) {
		if (payload) return {type, payload};
		else return {type};
	}
	return create;
}

I was wondering if you had any suggestions for a better solution?

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.