shakyshane / action-typed Goto Github PK
View Code? Open in Web Editor NEWBetter type safety from less typing - making Redux work more like Elm
Better type safety from less typing - making Redux work more like Elm
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.