Understanding and Embracing TypeScript’s “readonly”

If you’ve tried to use JavaScript in a functional style, you’ve no doubt chafed at the fact that all those little objects flying around at any given time are about as far as they can possibly get from immutable. You can, of course, be careful to write code that never mutates an object, but while good practices improve your code, they’re weak defenses against bugs.

TypeScript can help you with its readonly property modifier. With readonly, you can rely on TypeScript’s static analysis to enforce and flag mutations in your codebase.
Read more on Understanding and Embracing TypeScript’s “readonly”…

A New Redux Action Pattern for TypeScript 2.4+

In September 2016, I wrote a post about a pattern for strongly typed Redux reducers in TypeScript. Since then, the TypeScript team has been busy enhancing the language, and recent changes in TypeScript have enabled a much more natural, boilerplate-free pattern for strongly typed actions and reducers in TypeScript.
Read more on A New Redux Action Pattern for TypeScript 2.4+…

Making Toast from Scratch in React-Redux

Getting UI elements for free is a good feeling. When it comes to popup notifications, other frameworks provide these components for free. For instance, Rails has flashes and Android has snackbars. React is a moving target, so pulling in external libraries can help simplify situations, but it may prevent extensibility later.
Read more on Making Toast from Scratch in React-Redux…

DateStr – A Strongly-Typed Date String for TypeScript

Nearly every project I have ever worked on has had to deal with dates in one way or another. For example, there might be a need to generate a report for a specific date range. This would require a user to select a start and end date, which would be sent over the network to an API. The API would use those dates in an SQL query to retrieve the correct results. Read more on DateStr – A Strongly-Typed Date String for TypeScript…

Five Things to Think About When Considering TypeScript

I can’t imagine working on another JavaScript project without TypeScript, and I haven’t heard much talk of teams adopting TypeScript and regretting it. (For every hour you spend on TypeScript overhead, you probably save five hours of tracking down runtime errors.)

Nevertheless, I do think there are some conditions that could make TypeScript less valuable for a particular project or team. In this article, I’m going to talk about the things you ought to consider when weighing whether or not to add TypeScript to your project.  Read more on Five Things to Think About When Considering TypeScript…

Modular, Type-safe Metadata with TypeScript

One of the humps I’ve encountered while learning TypeScript is coming to terms with a core tenet of the language—the type definitions you write only exist at compile time, not at runtime.

I’ve bumped into this a few times. Can I generate a predicate function to test for type compatibility? No—use type guards instead. Can I reflect on the properties of a type at runtime? No—with caveats. It turns out that those caveats matter a lot, and you can do some pretty powerful things with the tools TypeScript does provide. Read more on Modular, Type-safe Metadata with TypeScript…

Typesafe Container Components with React-Redux’s Connect and TypeScript

TypeScript is fantastic—you get feedback as you write your code to (mostly) ensure you’re passing valid arguments to functions and props to components. However, when using the connect function, you may find that you are permitted to pass invalid props or omit required props to components. In this post, I’ll discuss how the issue arises, and how to fix it by typing your calls to connect! Read more on Typesafe Container Components with React-Redux’s Connect and TypeScript…

Representing Function Properties in TypeScript

We’ve been using TypeScript on an Electron project. It’s been a huge win already—a little additional upfront investment gives us more confidence that our code is correct and reduces the chance that it will pass unexpectedly-shaped objects around, a source of many bugs in my past Node applications.

But sometimes, it’s not immediately clear how to type certain kinds of objects. You can, of course, represent these as any whenever you need to—but any any you rely on can weaken your code’s quality. Last week, I discovered another way to avoid falling back on that crutch, thanks to the power of TypeScript’s type system.
Read more on Representing Function Properties in TypeScript…

Factory.ts: A Factory Generator for Test Data Using TypeScript

I’ve been using TypeScript on a React/Redux project, and I’m really enjoying it.

A year and a half ago, I tried to use TypeScript with an Angular project, and I found that it didn’t add that much. But with version 2.0 and on, TypeScript has really come into its own. Structural typing allows you to express concepts in TypeScript that I’ve never been able to express before. In particular, mapped types are just insanely useful.
Read more on Factory.ts: A Factory Generator for Test Data Using TypeScript…