The Lies We Tell Ourselves Using TypeScript

Rate this content
Bookmark

How safe is TypeScript's type safety? How much can you trust your statically typed code? Can you even consider TypeScript's type system "strong"? In this talk, we look at situations where TypeScript fails badly and learn why things have to be that way. We talk about trade-offs, workarounds, and ultimately solutions for all the damn, terrible lies we tell ourselves when using TypeScript.

FAQ

The speaker suggests several solutions, such as using conditional types, enforcing stricter type checks with 'unknown' rather than 'any', and utilizing generics to ensure type accuracy and safety.

The talk focuses on challenging common misconceptions and over-reliances on TypeScript, highlighting potential pitfalls and errors that can occur despite TypeScript's robust typology system.

TypeScript is designed to catch type errors at compile time, but the speaker demonstrates scenarios where TypeScript's type system checks out during compilation yet still leads to runtime errors.

Common problems discussed include TypeScript's handling of 'any' and 'unknown' types, the pitfalls of type assertions, and issues with function overloads and generics that can lead to type mismatches and operational errors.

Despite identifying various issues and limitations within TypeScript, the speaker expresses a strong preference for TypeScript, praising its productivity benefits and its balance between correctness and developer productivity.

TypeScript is preferred because it prioritizes developer productivity with a balance of type correctness and usability, whereas Flow's focus on a provably correct type system was found to hinder productivity, leading to its lesser popularity.

The speaker advises being pragmatic and aware, emphasizing the importance of understanding and learning TypeScript's behavior and trade-offs to make well-informed decisions in software development.

 Stefan Baumgartner
Stefan Baumgartner
28 min
21 Sep, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Talk discusses the limitations of TypeScript and the lies we tell ourselves about its type safety. It explores examples of unsafe operations and bending the type system using keywords like type assertion and function is dice. The Talk also covers catching syntax errors, type mismatches, and the complexity of conditional types and generics. It emphasizes the importance of understanding TypeScript's limitations and making well-informed decisions as software engineers.

1. Introduction to TypeScript and Its Limitations

Short description:

Welcome to my talk, Lies We Tell Ourselves Using TypeScript. It has been a fantastic TypeScript congress so far. Today, I talk about an example that TypeScript is totally happy with. We're just fetching some data from an API and concatenating it with something else. TypeScript says it's good code, but it will most likely crash at runtime. Let's go through the code step by step.

Hi, welcome to my talk, Lies We Tell Ourselves Using TypeScript. It has been a fantastic TypeScript congress so far. You have seen lots of fantastic speakers, people that I wanted to meet for such a long time, people that I've engaged with on social media, and who are the best of the best in TypeScript.

Now, I need to tell you that I'm sorry. I'm sorry because you had fantastic talks from fantastic speakers. Now, I'm going to crush your dreams and destroy your hopes because this is not going to be a fun talk. I've done a fair share of TypeScript development. I've written two books about it, one which is TypeScript, the good parts. This is how the type system works for types for JavaScript developers. The other one TypeScript Couple where they have 100 actual problems that we are going to solve together.

And throughout my journey, I always start with this example where TypeScript is absolutely perfect for. I have this JavaScript codes. Don't worry about what it does, but it's just JavaScript. The browser executes it. It just doesn't work. There's no error that's being flown. Nothing tells you what's going wrong. It runs, it doesn't throw any errors, but it does not produce the right results. And for examples like that, TypeScript is fantastic because TypeScript, just when I activate it, finds about, I don't know, 10 errors in 15 lines of code. That's what we're here for. That's TypeScript's purpose. But this is not what I'm talking about today.

Today, I talk about the total opposite. I'm going to show you an example that TypeScript is totally happy with. We're just fetching some data from some API and concatenating it with something else. And TypeScript says, well, the types check out. This is good code. It compiles, you can ship it, and it will most likely crash at runtime. You know, at runtime, those things that TypeScript is supposed to prevent. Let's go through the code step by step.

2. Fetching Data and Handling Errors

Short description:

The first function fetches data from the Star Wars API using function overloads. We unwrap the fetched data and handle any potential syntax errors. Then we concatenate arrays of people or species using the push method, and finally, we create a new people array and append species to it. This program has about 10 lines of code and we will now address the problems step-by-step, starting with the core of our structure.

The first function fetches data. We call it list entries, and you have two ... you have two function overloads that define an API for you and your users. If you get a kind of species, then you're getting a promise of species in return. If you're getting a kind of people, then you're getting a promise of people in return. We are accessing the Star Wars API, which is great if you want to do any rest tests. And you have a very tailor made API so that you actually know what you should get back. Then you have the third function overload, which is the actual function implementation for you.

Next, we are fetching data and since it has some meta information, we are unwrapping it. We are just interested in the results. One thing that is very interesting about this line of code is that we are doing some, sorry, that we are doing some error handling. So there's one part where we say, well, that result.JSON call on line three might go wrong. So let's better catch that syntax error. What if we don't get any JSON back? Fantastic. Five lines of code for that function, three lines of type information.

Next is one line. I just formatted a little bit for this slide. Then we are concatenating a promise of an array of people or species to another list of array of people or species. So we're concatenating those two with the part push method of an array. And finally, we are calling so we are creating a new people array and we are pending species to the people array. All right. This is our program. It's about, I don't know, 10 real lines of code, a couple of type information. And I want to ask you if you look at the entire program, do you know what's going wrong or do you know what's supposed to go wrong? So, well, I think everything. And where does it go wrong? Everywhere. And when does it go wrong? Yeah, one by one, because we're single threaded. But don't worry, we are going through it step-by-step. We are now going to look at all our problems. Problem number one in chapter one, a tail of fetch. Let's look at the core of our very first structure.

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.
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.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Why are we devs so obsessed with decoupling things that are coupled nature? tRPC is a library that replaces the need for GraphQL or REST for internal APIs. When using it, you simply write backend functions whose input and output shapes are instantly inferred in your frontend without any code generation; making writing API schemas a thing of the past. It's lightweight, not tied to React, HTTP-cacheable, and can be incrementally adopted. In this talk, I'll give a glimpse of the DX you can get from tRPC and how (and why) to get started.

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.