Lessons from Maintaining TypeScript Libraries

Rate this content
Bookmark

Maintaining widely-used JS libraries is already complicated, and TypeScript adds an additional set of challenges.

Join Redux maintainer Mark Erikson for a look at some of the unique problems TS library maintainers face, and how the Redux team has handled those problems. We'll cover:

- Tradeoffs of different ways to define TS types for a library
- How to target different versions of TS, and considerations for determining the supported version range
- Migrating existing JS libraries to TS
- Differences between writing "app" types and "library" types
- Managing and versioning public types APIs
- Tips and tricks used by types from the Redux libraries
- TS limitations and possible language-level improvements

FAQ

Types in TypeScript serve several purposes such as API documentation, enhancing user and library code correctness, and improving maintainability by ensuring that the library behaves as expected.

Best practices include setting up build infrastructure, ensuring TypeScript types are compiled and included in the package, using existing typedefs as a starting point, converting and testing files incrementally, and making sure to export types from the index file.

Versioning TypeScript types involves considering types as APIs, factoring them into versioning decisions, and distinguishing between breaking and non-breaking changes. It's important to align with a consistent policy like the one suggested by the Ember team in their RFC.

Supporting multiple TypeScript versions can be achieved by using CI to build and test against different versions, possibly using older TypeScript versions during development, and utilizing the 'typesVersions' field to direct TypeScript to use specific type definitions based on the version.

Challenges include handling the dynamic nature of JavaScript and complex type usage such as generics and conditional types. Solutions often involve simplifying APIs, like the Hooks API in React Redux, to make them easier to type and use.

Maintainers can debug types by recreating complex types step-by-step, using tools like 'Any.compute' for recursive expansion, and testing types through TypeScript code that compiles cleanly with assertions on expected types.

The lack of semantic versioning in TypeScript means that new releases could potentially break existing code. Library maintainers need to carefully manage their type definitions and consider even minor updates as potential breaking changes.

Mark Erikson
Mark Erikson
30 min
29 Apr, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Mark Erickson, a Senior Frontend Engineer at Replay, discusses JavaScript libraries and their support for TypeScript, including migration, versioning, and debugging. He also explores the challenges of supporting multiple TypeScript versions and designing APIs for use with TypeScript. Additionally, he shares advanced Redux type tricks and insights into maintaining a TypeScript library. The poll results reveal the widespread usage of TypeScript among developers, with many gradually migrating their codebases. Lastly, he provides tips for upgrading TypeScript and verifying functionality.

1. Introduction to Mark Erickson

Short description:

Hi, I'm Mark Erickson, a Senior Frontend Engineer at Replay, known for answering questions about React and Redux, collecting helpful links, writing blog posts, and being a Redux maintainer.

Hi, I'm Mark Erickson, and today I'd like to talk to you about lessons I've learned maintaining TypeScript libraries. A couple quick things about myself. I'm currently a Senior Frontend Engineer at Replay, where we're building a true time-traveling debugger for JavaScript applications. If you haven't seen it, please check it out. I'm known for a number of things. I am an answerer of questions. I will happily answer questions about React and Redux anywhere there is a text box on the internet. I collect interesting links to anything that seems potentially helpful. I write extremely long blog posts about React and Redux, and I am a Redux maintainer. In addition, you might also know me as that guy with the Simpsons avatar.

2. JavaScript Libraries and TypeScript Support

Short description:

Today we're going to talk about different ways that JavaScript libraries can use and support TypeScript, how you approach migrating a JavaScript library to TypeScript, issues with dealing with the library types and versioning, and supporting multiple TypeScript versions as well, how you can debug and test types, how you approach designing APIs for use with TypeScript, and potentially some features that would make it easier for TypeScript usage with libraries. Types serve several purposes, including API documentation, user code correctness, and library code correctness. There is a distinct difference between application types and library types, with library types being more complex. JavaScript libraries can provide types by being written in TypeScript, hand-writing type definitions for JavaScript code, or using community types from Definitely Typed. The Redux libraries have used all these approaches and have been migrating to TypeScript. The first step in migration is setting up build infrastructure and configuring tests.

Today we're going to talk about different ways that JavaScript libraries can use and support TypeScript, how you approach migrating a JavaScript library to TypeScript, issues with dealing with the library types and versioning, and supporting multiple TypeScript versions as well, how you can debug and test types, how you approach designing APIs for use with TypeScript, and potentially some features that would make it easier for TypeScript usage with libraries.

So why do we even provide types with a library anyway? Types serve several purposes. One is API documentation. Users can look at the types and understand what functions and types exist and how you can use them in your application. Another is user code correctness. They can enforce certain usage patterns that users should have in their actual application source code. Along with that, there's library code correctness. Types help us ensure that the code inside our library behaves as expected, and it's about maintainability, being able to work on the actual code inside the library.

Now, I will say that I think there is a distinct difference between the types that you see inside application code and the types that you see inside library code. Application types tend to be fairly simple. You have API responses, function arguments, state that you're dealing with, component props. Usually, it's not overly complicated, and you don't see a lot of generic types in there. Library types, on the other hand, are much more complicated because they need to handle a lot more flexible use cases. Library types tend to have a much heavier use of TypeScript generics. And sometimes, you might even see type level programming where you're doing inference, conditional logic, and complex transformations of types as well.

Now, there are several ways that a JavaScript library can provide types. The best approach is if the library is actually written in TypeScript itself. This guarantees that the types match the actual behavior of what's in the source code of the library, and that the types get updated every time there's a new release of the lib. Another option is to write the source code in JavaScript and handwrite the type definitions and include them in the published output. This is okay because the types are still maintained by the actual library owners. But you can actually end up in situations where there's differences between what the types say is in the library and what the source code actually does. As a fallback, if the library maintainers don't want to have their own types, then the community can put together some types and publish them in the Microsoft-owned Definitely Typed repo. This can definitely lead to problems, but at least it gives you some way to have types, especially if the library maintainers don't want to worry about dealing with that themselves. We've really used all these different approaches with the Redux libraries over time. But we've been working on trying to migrate the Redux libraries to TypeScript, especially over the last couple of years. The Redux core actually got converted to TypeScript in 2019. We just never actually got around to publishing it. React Redux version 8 is finally converted to TypeScript, and we migrated Reselect last year. So how do you approach doing one of these migrations? Well, the first step is to get some build infrastructure set up. You need to make sure you're actually compiling the TypeScript types, transforming the build output to plain JavaScript, and you want to make sure your test setup is configured to deal with TypeScript as well.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
TypeScript and React are inseparable. What's the secret to their successful union? Quite a lot of surprisingly strange code. Learn why useRef always feels weird, how to wrangle generics in custom hooks, and how union types can transform your components.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
We don't think of React as shipping its own types. But React's types are a core part of the framework - overseen by the React team, and co-ordinated with React's major releases.In this live coding talk, we'll look at all the types you've been missing out on. How do you get the props type from a component? How do you know what ref a component takes? Should you use React.FC? And what's the deal with JSX.Element?You'll walk away with a bunch of exciting ideas to take to your React applications, and hopefully a new appreciation for the wonders of React and TypeScript working together.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
The more you keep working on an application, the more complicated its routing becomes, and the easier it is to make a mistake. ""Was the route named users or was it user?"", ""Did it have an id param or was it userId?"". If only TypeScript could tell you what are the possible names and params. If only you didn't have to write a single route anymore and let a plugin do it for you. In this talk we will go through what it took to bring automatically typed routes for Vue Router.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
I'll dive into the internals of Nuxt to describe how we've built a TypeScript-first framework that is deeply integrated with the user's IDE and type checking setup to offer end-to-end full-stack type safety, hints for layouts, middleware and more, typed runtime configuration options and even typed routing. Plus, I'll highlight what I'm most excited about doing in the days to come and how TypeScript makes that possible not just for us but for any library author.
How to Build Your Own Open Source Project
React Advanced Conference 2022React Advanced Conference 2022
16 min
How to Build Your Own Open Source Project
We all used open source projects every day such as npm packages, editors, web applications, and even operating systems... Have you ever thought of building one of your own? In this talk, I will share my journey building jest-preview, from when it was just a vague idea, to currently a well-adopted library to help frontend engineers write tests faster. I will share with you how to come up with an idea for a project to work on, what is the struggles you have to overcome as an author of an open source project, how to manage time efficiently, and how you get attention from engineers around the world.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
Type-checking a TypeScript codebase can be slow, especially for monorepos containing lots of projects that each need to use the type checker to generate type declaration files. In this talk, we introduce — for the very first time — a new TypeScript feature we are working on called “Isolated Declarations” that allows DTS files to be generated without using the type checker at all! This opens the door to faster declaration generation in TypeScript itself, as well as in external tools written in other languages such as ESBuild and swc. You'll see how to use this new option, and maybe (just maybe) you’ll be convinced about the benefits of explicit return types! Most importantly, we will show how Isolated Declarations enables parallel builds to spread work across your CPU cores to significantly improve the build speed of your TypeScript projects.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Practice TypeScript Techniques Building React Server Components App
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
Workshop
Maurice de Beijer
Maurice de Beijer
In this hands-on workshop, Maurice will personally guide you through a series of exercises designed to empower you with a deep understanding of React Server Components and the power of TypeScript. Discover how to optimize your applications, improve performance, and unlock new possibilities.
 
During the workshop, you will:
- Maximize code maintainability and scalability with advanced TypeScript practices
- Unleash the performance benefits of React Server Components, surpassing traditional approaches
- Turbocharge your TypeScript with the power of Mapped Types
- Make your TypeScript types more secure with Opaque Types
- Explore the power of Template Literal Types when using Mapped Types
 
Maurice will virtually be by your side, offering comprehensive guidance and answering your questions as you navigate each exercise. By the end of the workshop, you'll have mastered React Server Components, armed with a newfound arsenal of TypeScript knowledge to supercharge your React applications.
 
Don't miss this opportunity to elevate your React expertise to new heights. Join our workshop and unlock the potential of React Server Components with TypeScript. Your apps will thank you.
Advanced TypeScript types for fun and reliability
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
Workshop
Maurice de Beijer
Maurice de Beijer
If you're looking to get the most out of TypeScript, this workshop is for you! In this interactive workshop, we will explore the use of advanced types to improve the safety and predictability of your TypeScript code. You will learn when to use types like unknown or never. We will explore the use of type predicates, guards and exhaustive checking to make your TypeScript code more reliable both at compile and run-time. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Are you familiar with the basics of TypeScript and want to dive deeper? Then please join me with your laptop in this advanced and interactive workshop to learn all these topics and more.
You can find the slides, with links, here: http://theproblemsolver.nl/docs/ts-advanced-workshop.pdf
And the repository we will be using is here: https://github.com/mauricedb/ts-advanced
Mastering Node.js Test Runner
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Marco Ippolito
Marco Ippolito
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.