3 ms·
Yes, I'm doing basically the same (define constant a as literal type, then use "typeof" operator to define action type, and then create alias for all Actions us
by mpawelski 10y ago
Yes, I'm doing basically the same (define constant a as literal type, then use "typeof" operator to define action type, and then create alias for all Actions using union type).
That's one of the reasons why I miss possibility to tell what is the type of the object that function returns. So I could do something like this (hypothetical syntax)
const INCREMENT: "increment" = "increment";
const DECREMENT: "decrement" = "decrement";
function increment(inc: number) {
return {
type: INCREMENT,
inc
};
}
function decrement(dec: number) {
return {
type: DECREMENT,
dec
};
}
type Actions =
returntypeof increment
| returntypeof decrement;
So I won't need to write action type when I have action creator method.
I hope that in future Typescript devs will add some feature to allow this kind of pattern.