Type System React

Rate this content
Bookmark
Slides

TypeScript's type system is incredibly powerful. It can represent bizarrely complex interdependent JavaScript types and comes with a Turing-complete set of logical conditions. But this is a React conference, right? Let's implement a primitive React purely in the type system. For fun!

This talk has been presented at React Day Berlin 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Josh Goldberg is a full-time open source maintainer in the TypeScript ecosystem and the author of the book 'Learning TypeScript' through O'Reilly.

Type System React is a GitHub repository by Josh Goldberg that explores advanced TypeScript concepts by implementing a type system equivalent of React's JSX engine without runtime code.

TypeScript ESLint is a tool that allows you to run standard JavaScript tools such as ESLint and Prettier on TypeScript code.

The code is available on GitHub under the repository 'Type System React' in Josh Goldberg's user account, specifically in the 'source/fun' directory.

No, the concepts covered in Type System React are not intended for everyday TypeScript development. They are advanced techniques meant for deep exploration of the type system.

The TypeScript playground is an online tool available at typescriptlang.org/play, where you can write TypeScript code and see the resulting JavaScript code or any errors.

The render component type in Type System React takes in a tag and children as type parameters and returns a resultant type based on the component registry.

Josh Goldberg recommends the TypeScript playground, his book 'Learning TypeScript,' and Matt Pocock's totaltypescript.com for learning TypeScript.

DefinitelyTyped is a large public open-source repository where third-party types for packages that do not describe their types are stored. It helps improve TypeScript compatibility for various packages.

Type challenges are exercises available on the Type Challenges repository that help you practice and flex your TypeScript type system skills, starting from basic to more complex challenges.

Josh Goldberg
Josh Goldberg
21 min
12 Dec, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk is about implementing a limited subset of the type equivalent of React, specifically its JSX engine, in the TypeScript type system with no runtime code. The speaker demonstrates how to use TypeScript features like constrained types and template literal strings to infer and render JSX elements in the type system. They also show how to render headings and children using a utility called 'render component'. The Talk concludes with additional resources for learning TypeScript and related topics.
Available in Español: Sistema de Tipos React

1. Introduction to Type System React

Short description:

Hello and welcome to Type System React with me, Josh Goldberg. I am a full-time open source maintainer in the TypeScript ecosystem. We're going to implement a limited subset of the type equivalent of React, just its JSX engine in the type system with no runtime code. Let's begin in TypeScript lang dot org slash play.

Hello and welcome to Type System React with me, Josh Goldberg. I am a full-time open source maintainer in the TypeScript ecosystem. I work on general projects that help you write TypeScript a little better, most notably TypeScript ESLint, the tool that lets you run standard JavaScript tools, such as ESLint and Prettier on TypeScript code.

I'm also the author of the learning TypeScript book through O'Reilly, so I like talking about TypeScript. Everything we're going to chat about today is on GitHub and open source under Type System React, a repo in my Josh Goldberg user, but I want to note that this is not normal TypeScript. You don't need to follow along precisely and get everything fully to be a TypeScript dev or to work proficiently in TypeScript. This is all shenanigans today, and while the concepts I'm going to show are actually really useful for working in the type system if you do it a lot, it's not stuff that you should be using day-to-day. It's all silly stuff, all the weird shenanigans.

Because what we're going to do is implement a very limited subset of the type equivalent of React, really just it's JSX engine in the type system with no runtime code. We're going to make a type, let's say a component registry. We're going to make a helper type render that takes in a string and gives back the JSX results, the rendered results, and we're going to print that out to the developer tooling stuff that's running TypeScript for us. So very much not practical. You would never satisfy any user needs with this, but I think it's a cute way to explore the type system.

So let's begin. You can find all the code I'm going to send my live code under the repo under source slash fun, but I'm going to be working in TypeScript lang dot org slash play, which is a really nice playground available on the TypeScript website. You can type in TypeScript code on the left and get back JavaScript code or any errors, declaration files on the right. Now on the left, I have just some standard TypeScript code. I have a type component registry and a console log, and we can see that the type system representation just includes the type and the JS representation just includes the JavaScript. But I mentioned that today is purely in the type system. So I'm just going to go ahead and remove our console log. No JavaScript just types. But that brings up the question of how are we going to print things. So I'm going to make this little temporary type print me here. I'm going to say I want to print component registry. And there's actually a nice little TypeScript playground and other TypeScript editors feature called to slash assertions. You can get an extension of VS Code to do this. But if you write a comment that has this little caret and a question mark, it will ask it to print to the screen what you would have gotten if you were to hover your mouse there. So here we got type print me as the component registry type. And printed out, we got the stringified version of that. Emoji is sparkling heart.

2. Exploring Tag Rendering and JSX in TypeScript

Short description:

I'm going to switch to sparkles and heading is H1 with children. I want to be able to get a tag name and render out the contents under that tag. TypeScript has a feature called a constrained type to ensure we're only passing in one of the actual keys. I'm going to extract that out to a component type so that whenever I say component, what I really mean is one of the keys of the registry. I want to be able to have JSX and self-closing tags, which is not supported currently.

I'm actually going to switch to sparkles, a little less glaring, and heading is H1 with children. Hooray. Printing in the type Printing in the type system.

But I need to do more than that. I want to be able to, let's say, get a particular tag name and render out the contents under that tag. And you can do that in TypeScript with this index signature lookup, this little array type. Here we're saying give me under component registry the type under the emoji property name, which in this case, yes is sparkling hearts, pardon me, sparkles. If we were to switch that to heading, we'd get H1 children slash H1. If we were to switch that to, I don't know, ASDF WAT, some gibberish, we'd get Red Squiggly's property ASDF WAT does not exist in type, component registry.

Cool. And that little constraint there, the fact that it needs to be one of the actual keys of the type is useful because I want to write a render type which takes in a type parameter and gives me back component registry of that type parameter, making a kind of dynamic version of what I'm doing here. If I were to say want to render emoji, this fun fact actually works. This is what's called a generic type or type with a type parameter. It's kind of like a function in the type system. We take in a tag, say emoji, the string, and then we do something. We create a new type with that tag. Here we're making component registry of tag as a result, but we're getting the complaint type tag cannot be used to index type component registry. In fact, if we switch to the errors tab here, we can see that. Well, that makes sense because what if I passed in asdf? We need some way of making sure we're only ever passing in one of the actual keys of this type. And TypeScript has a feature for that, it's called a constrained type. With the extends keyword, we can extend key of component registry. Voila, no more red squigglies. Print me is happily the sparkling emoji, and here we're saying tag extends or must be one of the keys of component registry. I'm actually going to extract that out to a component type here so that whenever I say component, what I really mean is one of the keys of the registry. If we were to switch this to heading, yep, it renders the heading nicely. Rendering, it's a start. But I want to be able to do more than just take in a tag name and render under the tag. I want to be able to have JSX. I want my little self-closing tags. I want to be able to do something like emojis self-closing, which right now is not supported.

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.