One bad habit of mine is to share materials to make a point instead of making a point myself. But my attempts usually weren’t successful despite the merits of those materials. I wondered why people didn’t see the “light.” But, recently, why sharing articles, reports, or some data rarely persuades anyone clicked in my head.
Persuasion and following commitment do not originate from our rational mind especially if the problem space is complex and the benefits and the costs are not obvious. Rather, they happen at the emotional level. So you change behaviors when you have others feel your pain.
For example, if I want to avoid slow review turnaround time and resulting big pull requests, the best way to persuade is to vividly describe how I feel that longer review cycles slow down my projects.
It is not easy to do, I know. I also keep forgetting this. But next time I feel the urge to share an article, I will take a step back and think about the pain I have at the moment.
Recently I was appalled to hear that my friend, who is a software engineer, was denied an internal transfer because his potential manager did not want a non-native English speaker on his team. As a native Korean speaker myself, it was unsettling to hear such a story. I thought that in this day and age, especially at Silicon Valley, we have figured out how to work together with those from different backgrounds.
I realize I don’t see many conversations around how this lack of fluency impacts some of us. I suspect that’s because most writers and speakers are already fluent in their languages. So we don’t talk about this issue and put the onus on the individuals.
We can’t expect people who spent most of their lives in different languages to understand all connotations and cultural subtext. It is not going to happen no matter how hard they try. Even some native speakers have a hard time communicating well. If we believe that communication is a two-way street, we have to meet in the middle.
Yes, I get that it makes communication more difficult. But we, collectively speaking, hired each of us because everyone was deemed qualified to do the job. Then, it is on all of us to create an environment where we all can be successful.
How? I admit I don’t have a great idea. But I will start by setting up 1:1s to build the relationship and the context. That will provide a sometimes necessary bridge for the lapses. If I notice how well or badly we communicate, I will share that too. And I will always try to remember that none of us is perfect. Language proficiency, or the lack thereof, is just another imperfection some of us have.
A caveat I found is that Promise.allSettled will never reject. It does make sense but at first I found myself thinking, “so when does it reject and what does it reject with?” I am interested to see how this behavior will be typed in Typescript.
This came up during a meeting to review how we organize our work (basically JIRA).
In this day and age, everything is linkable; whether it be a Slack thread, a Zendesk ticket or a Google Doc. So in order to give “context,” it is tempting to just throw a link in a conversation. But you can do more than that.
I wouldn’t say linking does not have its own value because discovery is a real problem. However, every one of us is overloaded with information. We can’t be expected to go down every rabbit hole of links. So, help us out a little. Give us a link, and summarize it a little for us: What’s in it? Why do you think this context is useful? This will save us good 10-20 minute for every link and we will appreciate you going extra mile for us.
When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form. –HTML Spec
I was looking into a bug that an embedded form would die due to the security restriction when you press enter inside the input. It turns out this random behavior was causing the issue 🤷
(I concede that we should handle form submit properly though)
Complexity is the bane of all software projects. Not all code may be debt, but all will increase the complexity. There are two ways to fight this complexity. We can enhance our ability to handle complexity with new abstractions, thorough tests and more documentation (the engineering way). Or, we can write less code (the product way).
My go-to approach has been to the engineering way. To build enough safety nets to protect myself from the complexity. This requires less product deliberation and promotes a bias towards action (“as long as things are tested, we are good”). However, I tend not to think critically about new proposed features. This article on saying “no” reminded me of the other half of the methods I have ignored.
I often find myself that I react very defensively when I chat over Slack vs. in person. While reading this article on Consious Leadership, I just realized why: when I am just looking at a screen, I forget that I am talking to a human being. The discussions feel more abstracted and more about ideas and I lose my empathy. It is good to know why and now I can improve upon it.
My first reaction was “well, you should use GraphQL.” Or, even if you don’t have control over your APIs, having two sources of truth feel very cumbersome just to validate data. I believe a better approach will be to generate a JSON validator based on the type information at compile time such as babel-blade, or react-docgen-typescript-loader. But then again, I don’t have a clear plan to achieve that, either.
In this part two, I am going to describe our team’s current best practices to make Typescript work for you when working with Redux.
Creating Type-safe Actions and Reducers
Properly typing Redux Container
Creating Type-safe Actions and Reducers
Considering how reducers are just simple functions that accept two arguments, you would expect Typescript to work well with those two. States do. But actions, because dispatch accepts any types of arguments, cannot be typed safely without developers’ involvement. If you don’t type your actions, your reducer will end up in the not-so-ideal state:
You can catch some of type errors with unit tests, but you will miss some properties and lose easy refactoring provided by Typescript. To acheive type-safety before Typescript 2.8, you could use string enum:
IOtherAction is needed so that Typescript won’t complain about default case in switch statement (that is, exhaustiveness checking). This works OK if you ignore the fact that there are essentially two duplicate type definitions in your action interfaces, and action creators. Starting with Typescript 2.8, you can use ReturnType to remove action interfaces. The code below is our way to type actions and reducers.
Before you try to type Redux container components properly, you need to understand the type definition of connect. Carefully read the code below I quoted from Redux type definition (comments are mine). The definition uses a lot of type overloading but I will go through some cases to help you understand what exactly goes on.
Please note that the definitions below are from @email@example.com.
When you don’t pass in any argument to connect
This is when you only need dispatch inside your container.
When you pass in both mapStateToProps and mapDispatchToProps to connect
This isn’t hard to understand once you understood how Redux type definition handles mapStateToProps. mapDispatchToProps is treated like mapStateToProps. For your reference, I included the overloaded type below.
This is also rather straightforward. Instead of merging TStateProps, TDispatchProps, and TOwnProps naively for the component definition, Connect will now depend on mergeProps to merge these props. The only additional check, (or inference) is whether mergeProps is of type (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TMergedProps;.
What this means
First of all, congratulations on getting through all these different types! Now you get how Connect works. But, it turns out you don’t need to type things directly when you use Redux’s Connect. However, other HOC’s definitions will vary, and you will need to learn how their type systems work.
Extracredit (Typescript tips not related to Redux)
Know your types in React
Knowing React types helps your code to work with React seamlessly. Here is the usual go-to list for us.
React.ReactNode=React.ReactElement+Renderableprimitivetypes(object is notvalid). `children` hasthistype
This is an easy-to-miss option when you first start using Typescript. You should use typeRoots option to avoid adding unnecessary dependencies.
As we develop, and maintain our React apps, we have encountered many bugs. Based on our experience, the harder-to-track, and more critical bugs often stemmed from typeless part of the code. That is why we are determined to type things both comprehensively, and correctly. This isn’t the farthest we can go with Typescript, but this is where we are at, and I hope this article has helped you understand Typescript and Redux more deeply.
In this two-part post, I am going to go over the different flavors of Redux state management at Vingle and our thought process behind each iterations we went through over the last year and half. I hope this post guide how you structure your Redux states.
Genesis: Redux + Immutable.Map
My team chose React to create a small-scale mobile marketing website as a learning experiment. Our main project, at the time, was based on Rails, and Angular 1, and we were separating web applications from Rails to simplify, and speed up our deployment process. That meant we had to create everything from scratch: a new build pipeline, a new webpack configuration, while learning about the vast React ecosystem.
We heard that Redux simplifies debugging application states greatly, and, with the nightmarish memories of debugging Angular 1’s watchers, chose to adopt Redux. We also learned a bit about shouldComponentUpdate and React’s component lifecycle, and wanted to have an immutable state. I was already familiar with high-order immutable objects from my previous work (this), so Immutable.js was an obvious choice.
In the end, we have Redux setup looking like this:
Once we have gotten more used to React, and Redux, and proven that we could develop new features much faster on the mobile page, we started migrating our main web application to React. But unlike the proof-of-concept mobile page, this app would have dozens of routes and reducers, and much more complex components, so we chose to use Typescript for this app.
Unfortunately, Immutable.Map with different types of values (number, boolean, other Maps, or Lists, for example) does not play well with Typescript. The following is a Typescript definition of Immutable.Map:
So we looked for a better way to tie Typescript and Immutable.js together. Then we found that there was another Immutable class called Immutable.Record and a library called typed-immutable-record. With the library, we created a type-safe Immutable Record:
It took some time for us to understand how to scaffold Record interfaces correctly but we managed to create type-safe redux states with both dot notations, and helper methods like setIn or withMuations. However, as you can see from the code above, we had to create a large number of interfaces, especially when our states were deeply nested. Once we got the pattern down, it wasn’t difficult to follow the pattern but it was a lot of work which disincentivized our team to create smaller, and isolated reducers. But we didn’t know any better, so we carried on.
By using Readonly interfaces, the scaffolding is reduced to a quarter by removing RecordPart, Record, and recordify. However, there is a problem with this approach when you need to update deeply; the case above UPDATED_TITLE is such an example. During the conversion, we had some codes go out of hand like this:
We could solve this problem by adopting a deep merge library, but we feared that those libraries may not be type-safe. After giving some thoughts, we determined that the real problem was with the deeply nested structures of our states and planned to flatten the states by normalizing. Of the two popular normalizing libraries, redux-orm, and normalizr, we chose the latter for its simplicity.
Our final, and current version of redux looks like the following:
When I look back, part of me regret that we didn’t do more research which could have saved a lot of time; this collection of redux-related libraries would have been helpful, and normalizing is already in official Redux documention. However, part of me also feel like we would have never appreciated the utility of these libraries and techniques because we didn’t know the downsides of not using those libraries and techniques. And that is why I wrote this post; I hope you understand what problems lie ahead and save yourself some time.