I've been using React + Redux + Typescript stack and I've been quite happy with it (It's hard for me to image how you could do big refactoring without TS).
The fact that components props are verified by compiler instantly when I type is a big win. I believe with PropTypes you can't specifically describe what is the shape of your data, for example object with property of such name and the value of this property is object with other concrete properties. Or array of objects with specific property. In pure react you only have "React.PropTypes.object" and "React.PropTypes.array" in TypeScript you can say "{ foo: string, bar : { fieldNum : number}}" or "{foo: string}[]".
Some other observations:
- I use Typescript 2.0 RC, because it has union types [0] and it works great with Redux reducers. It's super cool!
- I don't miss spread object operator that much, it would be nice but I'm fine with some utils function that can even verify that I "override" property that exist is source object[1] (I mean creating new object with changed properties)
- What I really miss is the ability to say that what is the return type of the method. There is "typeof" operator in typescript for telling what is the type of particular object, I would like something like "returntypeof". Because now I always need to write the type of an action and very often it could be inferred from action creator function. Or I need to write type to describe what will be returned by Redux's "mapStateToProps" function, and sometimes this object can be big.
I have to say, I'm pretty confused about what the state of 2.0 is. There's been no official announcement and the latest stable version on npm is 1.8.10[1]. That being said the latest release on GitHub is 2.0.2, with no official release of 2.0 (or 2.0.1 for that matter) listed anywhere[2]. There's also no active milestone on GitHub for the 2.0 release, but there is for patch versions of 2.0? I'm sure this all just means 2.0 hasn't been released yet, but it's super confusing.
Once TS figures out the action type is INCREMENT, it can infer the type of the payload. You don't need the Actions type either, you can just receive action: Increment | Decrement.
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.
The fact that components props are verified by compiler instantly when I type is a big win. I believe with PropTypes you can't specifically describe what is the shape of your data, for example object with property of such name and the value of this property is object with other concrete properties. Or array of objects with specific property. In pure react you only have "React.PropTypes.object" and "React.PropTypes.array" in TypeScript you can say "{ foo: string, bar : { fieldNum : number}}" or "{foo: string}[]".
Some other observations:
- I use Typescript 2.0 RC, because it has union types [0] and it works great with Redux reducers. It's super cool!
- I don't miss spread object operator that much, it would be nice but I'm fine with some utils function that can even verify that I "override" property that exist is source object[1] (I mean creating new object with changed properties)
- What I really miss is the ability to say that what is the return type of the method. There is "typeof" operator in typescript for telling what is the type of particular object, I would like something like "returntypeof". Because now I always need to write the type of an action and very often it could be inferred from action creator function. Or I need to write type to describe what will be returned by Redux's "mapStateToProps" function, and sometimes this object can be big.
[0] https://github.com/Microsoft/TypeScript/wiki/What's-new-in-T... [1] https://github.com/Microsoft/TypeScript/wiki/What's-new-in-T...