Types Beyond TypeScript

Rate this content
Bookmark

Everybody seems to be talking about "types" these days. From the TypeScript language to type description utilities such as prop-types and Zod, developers expect clear descriptions of the shapes of their React components, data, and hooks. Let's talk about the mindset shift that's happened over the last decade, and where types are taking us over the next one.

Brief foundations: what is TypeScript, what "type safety" means, and setting up TypeScript in a React (Next.js) projectA history of how type safety has worked in React, starting with class components

Thinking in Type(s|Script): How modeling value shapes helps raise predictability and understandability, especially in the wildest and wackiest of React architectures.

TypeScript's Limitations: By design, TypeScript can only act as a development-time type system and enforce what that system can represent. We'll want to go over what can't and/or shouldn't be represented in that type system.

Raising the Runtime: Moving those type thoughts into your React runtime with programmatic frameworks or libraries such as tRPC and Zod - especially as they integrate with React metaframeworks like Next.js and Remix.

React Specifics: How this "types-first" theory works helps improve common parts of projects in the React ecosystem: from prop-types back in the day to REST or RPC endpoints, testing, and documentation today.

ESLint lint rules to catch common async and React code bugs - and why the language is designed to let you do those dangerous things in the first place.

Ecosystem future: where the TC39 types-as-comments proposal will -and won't- take types at a language-level for JavaScript in general and React apps specifically.

By allowing our types to be a reflection of the runtime reality, we embrace types-first thinking in designing code - making our code more clear to read and update. These better-define boundaries help in everything from better-defined React component boundaries to auto-generated client<>server API bridges. Hooray, types!

This talk has been presented at React Summit 2024, check out the latest edition of this React Conference.

FAQ

TypeScript is a superset of JavaScript that adds syntax for explicit type declarations, allowing developers to specify the intent behind values in their code. This helps manage and scale code more effectively by using the TypeScript type checker to catch type-related errors.

TypeScript improves code management by allowing developers to declare the intent behind values through types. This makes the code easier to understand and maintain, especially as the number of developers working on a project increases.

Some alternatives to TypeScript include Flow from Facebook, which is more targeted towards Facebook's use cases, and Ezno, an ongoing project attempting to rewrite a TypeScript-like language in Rust. However, TypeScript remains the most popular tool in this space.

TypeScript does not exist at runtime; its type annotations are lost during the transpilation to JavaScript. It also cannot fix poor app architecture or convoluted code. Types in TypeScript are meant to describe the code rather than exist within it during execution.

Well-defined schema validation libraries, such as Zod, YUP, and ArcType, allow developers to describe a schema for validating and describing data shapes. These libraries can be used to ensure type safety, autocompletions, and better security in code.

Type-checked linting refers to linting that uses the type information provided by TypeScript to perform more powerful and accurate code analysis. This helps catch more complex issues that regular linting might miss, leading to cleaner and more reliable code.

TypeScript should not become available at runtime as it is designed to be a development-time tool. However, there are libraries like ArcType that can add runtime type checking if needed for specific use cases.

The TypeScript type cycle refers to the phases developers go through when learning TypeScript: initial excitement and overuse of advanced features, followed by disillusionment when the code becomes too complex, and finally reaching a balanced, productive use of TypeScript.

Interfaces are often preferred in TypeScript because they semantically convey that they are defining an object shape. However, both interfaces and types can be used almost interchangeably, and some features are unique to each.

The future of TypeScript includes ongoing improvements and new features. For example, TypeScript 5.5 introduces inferred type predicates from function bodies. Future versions will continue to enhance capabilities and deprecate outdated syntax slowly.

Josh Goldberg
Josh Goldberg
31 min
14 Jun, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk explores the concept of types and their significance in software development, particularly in relation to TypeScript. It discusses the limitations and advantages of TypeScript compared to other tools like Flow and Ezno. The Talk emphasizes the role of types in bridging system boundaries and improving code quality. It also highlights the importance of type-checked linting and the future of ESLint. Additionally, the Talk mentions the benefits of faster and easier linting with projects like Biome and OXC, and recommends books for further learning.
Available in Español: Tipos más allá de TypeScript

1. Introduction to Types and TypeScript

Short description:

I'm Josh. I'm really excited to talk to you today about Types beyond TypeScript. We're going to talk about four sections. What are types? What is this whole thing? Just in case you haven't played with TypeScript or Flow yet. Second, I want to quickly cover what types aren't. I also really want to talk about the stuff types are surprisingly good at. First, what can inform types? Like how you can set up your code to be well-typed. And then lastly, what types can inform? How having well-typed code can help tools run better on your code.

Hey, everyone! Oh, sweet Jesus. That's not good. I'm Josh. I'm really excited to talk to you today about Types beyond TypeScript. Yes. As Phil said, I do open source. Feel free to talk to me after this whole thing about that. It's a wonderful world. The ecosystem is always getting better. I work on projects. Who here uses TypeScript ESLint? Yes. Who here uses ESLint in general? Yes. Who here has sworn at ESLint or TypeScript ESLint? Yes. Who here still uses Mocha? Okay. Cool. I'm not talking about Mocha today. I'm talking about Type stuff. We're going to talk about four sections. What are types? What is this whole thing? Just in case you haven't played with TypeScript or Flow yet. Second, I want to quickly cover what types aren't. Whenever there's a new tool, as you may have seen in other recent developments in life, people get really excited and they use it for things that may or may not be good fits. And they can sometimes give the tool a bad name. I also really want to talk about the stuff types are surprisingly good at. First, what can inform types? Like how you can set up your code to be well-typed. And then lastly, what types can inform? How having well-typed code can help tools run better on your code. Really cool stuff. Are we ready for all that? Yes! Friday late afternoon at a conference.

2. Types and TypeScript in JavaScript

Short description:

What are types? In order to answer that question, we have to ask, what is TypeScript? JavaScript is a syntax along with a vague specification on how to execute this syntax. The problem with this, though, is that there's no built-in way in JavaScript to declare the intent behind those values. Which is why we have TypeScript. TypeScript adds in a syntax on top of JavaScript. It's a superset, meaning it's all of JavaScript plus this new stuff, and that syntax allows us to say explicitly not just what the values are, but the intent behind those values. It's so useful that it begs the question, why don't we have other TypeScripts? There are three examples of tools that are in the same space as TypeScript but aren't as popular as it, and I want to quickly go over. First, just curious, who here used Flow at any point in time? Flow is a lot like TypeScript. It's from Facebook, but it's actually gone a different direction from TypeScript. It's still under active development. But instead of getting a lot of usage and going worldwide and making it so everyone should ever, who has ever wanted to use types could use it, Flow is a little more targeted.

What are types? In order to answer that question, we have to ask, what is TypeScript? In order to answer that question, we have to ask, what is JavaScript? What is JavaScript? At its core, JavaScript is this. JavaScript is a syntax along with a vague specification on how to execute this syntax. This is lovely. Look at this. We have a let thingy. We didn't used to have let. Used to have var. We have the ability to assign a variable to different values. The problem with this, though, is that there's no built-in way in JavaScript to declare the intent behind those values. The intent behind those values. There's no little thing you can put there to say this is only ever going to be, say, a string. And that's problematic because code gets really hard to work with at scale. Once you pass five developers, 50 developers, 500 developers, it gets really hard to manage that stuff. Which is why we have TypeScript.

TypeScript adds in a syntax on top of JavaScript. It's a superset, meaning it's all of JavaScript plus this new stuff, and that syntax allows us to say explicitly not just what the values are, but the intent behind those values. In this case, string. Now, because we have the TypeScript syntax, we can do things like the TypeScript type checker. Fun fact, written in TypeScript, and that's a CLI or API you can call to get little complaints saying, hey, you said this was going to be a string, but you gave it a number, you silly goose. That's not allowed. You can run the type checker through the language services for TypeScript, such as VS code, great editors, and those will give you the same complaints but in little red squiggly line form, type number not assignable to string. And that's really useful. It's so useful that it begs the question, why don't we have other TypeScripts? Why is TypeScript the only tool that seems to be in popular usage today for enforcing types in JavaScript code? Not that I said popular. There are other tools, but TypeScript has the mind share. Now there are three examples of tools that are in the same space as TypeScript but aren't as popular as it, and I want to quickly go over. First, just curious, who here used Flow at any point in time? A good chunk of us. Flow is a lot like TypeScript. It's from Facebook, but it's actually gone a different direction from TypeScript. It's still under active development. But instead of getting a lot of usage and going worldwide and making it so everyone should ever, who has ever wanted to use types could use it, Flow is a little more targeted.

QnA

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

React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
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
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
This talk discusses the performance issues in TypeScript builds and introduces a new feature called isolated declarations. By running the compiler in parallel and using isolated modules, significant performance gains can be achieved. Isolated declarations improve build speed, compatibility with other tools, and require developers to write types in code. This feature has the potential to further increase performance and may be available in TypeScript soon.
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
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.

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.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured 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.
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.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side. 
Frictionless Development With Unified Type System
JSNation 2024JSNation 2024
113 min
Frictionless Development With Unified Type System
Featured Workshop
Ejiro Asiuwhu
Ejiro Asiuwhu
Imagine developing where frontend and backend sing in harmony, types dance in perfect sync, and errors become a distant memory. That's the magic of TypeScript Nirvana!
Join me on a journey to unveil the secrets of unified type definitions, the key to unlocking frictionless development. We'll dive into:
- Shared language, shared love: Define types once, share them everywhere. Consistency becomes your BFF, errors your worst nightmare (one you'll rarely see).- Effortless coding: Ditch the manual grind of type checking. TypeScript's got your back, freeing you to focus on building awesomeness.- Maintainability magic: With crystal-clear types guiding your code, maintaining it becomes a walk in the park. More time innovating, less time debugging.- Security fortress: TypeScript's type system shields your app from common vulnerabilities, making it a fortress against security threats.