
I’m Jessica, 34 and a full-time student for the third time.
This time we are talking to Jessica who is all of the above and a wonderful mum. Let’s see how she spends.
Signing up takes 2 minutes. Scan this QR code to send the app to your phone.
Or head to the app store:
The benefits of Typescript in a team environment for building and maintaining production-worthy codebases are nothing new. At Cleo we committed early to developing our web and mobile apps with Typescript to give us the safety of static typing when developing our product.
One issue we faced building SPAs with Typescript was additional boilerplate when combined with other paradigms, specifically state management and Redux. The Redux docs[1] give a good starting point for Typescript integration, but their approach remains convoluted. We wanted a way of simplifying this integration to minimise the effort of achieving full type coverage within our codebase.
We'll create a simple app for displaying a customer's transactions to show how we can create type safety alongside Redux.
We define a new type for our transaction, and the store's state.
// types.ts
type Transaction = {
id: string;
description: string;
amount: number;
categoryId: Category['id'];
};
type State = {
transactions: Transaction[];
};
We use the typesafe-actions library to generate typed actions. Defining a new params type for when we dispatch the action.
// actions.ts
import { createStandardAction } from 'typesafe-actions';
import { Transaction } from "../types";
type SetTransactionDescriptionParams = { transactionId: Transaction['id'], description: Transaction['description'] };
export const setTransactionDescription = createStandardAction('SET_TRANSACTION_DESCRIPTION')<SetTransactionDescriptionParams>();
export const deleteTransaction = createStandardAction('DELETE_TRANSACTION')<Transaction['id']>();
The following shows the type safety we get when (incorrectly) dispatching actions:
Where typesafe-action starts to shine.
The dispatched action is typed with the ActionType helper. This builds a union type of the actions:
action: ActionType<typeof transactionListActions>
// evaluates to
{ type: 'SET_TRANSACTION_DESCRIPTION', payload: SetTransactionDescriptionParams } || { type: 'DELETE_TRANSACTION', payload: Transaction['id'] }
We use a switch statement on the action's type, and the getType helper to extract the action's type. This allows the payload's type to be correctly inferred & used within the scope.
// reducer.ts
import * as transactionListActions from './actions';
import { ActionType, getType } from 'typesafe-actions';
import { Category, Transaction } from "../types";
export const transactionListReducer = (state: TransactionListState, action: ActionType<typeof transactionListActions>): TransactionListState => {
switch (action.type) {
case getType(transactionListActions.setTransactionCategory): //getType evaluates to 'SET_TRANSACTION_DESCRIPTION'
// Payload is now typed correctly
const { transactionId, categoryId } = action.payload;
...
case getType(transactionListActions.deleteTransaction):
// Payload is now typed correctly
const transactionId = action.payload;
...
default:
return state;
}
};
The following shows the payload type suggestions we get when within a case.
Simply type the state
// selectors.ts
import { TransactionListState } from "./reducer";
import { Transaction } from "../types";
export const selectTransactions = (state: TransactionListState) => state.transactions;
export const selectCategories = (state: TransactionListState) => state.categories;
export const selectTransaction = (state: TransactionListState, transactionId: Transaction['id'] ) =>
state.transactions.find(transaction => transaction.id === transactionId);
This time we are talking to Jessica who is all of the above and a wonderful mum. Let’s see how she spends.
Things are tough right now and turns out, pandemics don’t spare your finances. On a mission to help where we can, we made Random Acts of Relief. Long story short – Michael gifted some money to someone in need and left this truly iconic note alongside it. We did what anyone with an aching desire for words of affirmation right now would do and hit him up for MORE.
Careers? Levelled up.