Comments (8)
My workaround would be passing an object with all the string literal
s like so:
//Over Simplified Version
// We have a "Factory" where we declare the shape of our ActionCreators
type Factory = {
a: (name: string) => /** ActionCreator */ ()=> { type: name };
b: (name: string) => /** ActionCreator */ ()=> { type: name };
};
// We have a object with the same keys as the factory where we pass the ActionTypes
type Names = {
a: "A";
b: "B";
};
// We have produce an object with the correct ActionCreators
type Result = {
a: () =>{ type: "A" };
b: () =>{ type: "B" };
};
// The shape of the function
function(factory): (names) => result
It introduces some boilerplate, but the types that it produces will have the correct literals.
from deox.
Sorry for being away. September was a busy month.
I will do a rewrite with the approach that you suggested soon as I get some time.
from deox.
Looks related to #66 (not exactly similar).
The main issue is string interpolation which generalizes the string literal
type to string
type. Do you find any solution for it?
from deox.
There are some workarounds for string literal
type operators in microsoft/TypeScript#12940 and microsoft/TypeScript#6579.
from deox.
I think we could tweak createActionCreator
to return a cleaner type, but I don't know if this would be a breaking change.
Should I experiment with it in that same branch or create a separate branch?
I think that ithe ActionCreator
type should have the following signature:
interface ActionCreator<
TAction extends string,
TCallable extends (...args: any) => any = (...args: any) => any,
TPayload = undefined,
TMeta = undefined
> {
(...args: Parameters<TCallable>): Action<TAction, TPayload, TMeta>;
type: TAction;
toString(): TAction;
}
from deox.
Hello. Maybe it is possible to implement this using Template Literal Types from Typescript 4.1+
from deox.
@Spaubleit Yes. At the time there was no recursive types nor template types on Typescript. I think implementing this should be quite easy at the current state. Thanks for bringing this issue to my attention
Right now I am using @reduxjs/toolkit slices, but a more strong typed alternative would be quite good
from deox.
I am closing this because I see no more benefit in implementing this. As much as the action type information looks nice in the linter, its often wrong (I say from experience because I implemented this in a separate fork). Also, see https://phryneas.de/redux-typescript-no-discriminating-union
from deox.
Related Issues (20)
- handleAction allows you to return a state object with any additional properties HOT 3
- 2.1.2 breaking change on ActionCreator type HOT 4
- The automated release is failing 🚨
- The automated release is failing 🚨 HOT 1
- ofType does no longer automatically infer action type from general source in pipe HOT 6
- createAction with only a type parameter results in function, which allows any arguments HOT 5
- Failed to resolve 'rxjs/operators' HOT 2
- Explain or remove `resolve => x => resolve(x)` pattern HOT 1
- Create polymorphic (generic) action creator HOT 2
- ESLint explicit-function-return-type rule error HOT 4
- Bundle size notes HOT 5
- Add "default" section in createReducer() HOT 4
- The automated release is failing 🚨
- Why was Deox deprecated? HOT 1
- The automated release is failing 🚨 HOT 1
- Prototype pollution in immer dependency HOT 2
- The automated release is failing 🚨
- Add Payload type utility HOT 1
- Is there a way to create request action generator? HOT 7
- Looking for a maintainer...
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 deox.