Unleashing the Power of Duck-Typing in React: Crafting a Generic Application

This ad is not shown to multipass and full ticket holders
React Advanced
React Advanced 2026
October 23 - 26, 2026
London, UK & Online
Upcoming event
React Advanced 2026
React Advanced 2026
October 23 - 26, 2026. London, UK & Online
Bookmark
Rate this content
Sentry
Promoted
Code breaks, fix it faster

Crashes, slowdowns, regressions in prod. Seer by Sentry unifies traces, replays, errors, profiles to find root causes fast.

Get started

As React developers we spend countless hours creating pages and components to bring our applications to life. It's an exhilarating journey, but have you ever wondered what happens when you're faced with the challenge of not knowing the components your page application will have?
Join me in this talk where we'll dive into the realm of TypeScript duck typing. Discover how this powerful technique can transform your React development mindset, and effortlessly construct a generic application that can serve an unlimited number of pages.

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

FAQ

David Villar-Marin is an Ecuadorian developer and chess player who has worked at Stack Builders, a consultancy focused on functional programming, and is also an open source contributor.

The main topic of David Villar-Marin's talk is creating a generic React application using TypeScript.

Duck typing is a concept in programming that brings flexibility and simplicity by allowing code to be loosely coupled, enabling reusability, modularity, and maintainability.

Duck typing can be implemented in TypeScript using type guards, which are functions whose return type is a type predicate, checking if an object has a specific property or set of properties.

In a React application, a switcher component conditionally renders other components based on the type of a variable.

Models in a generic React application serve as simple templates with type properties to ensure all components have consistent type properties, such as input, checkbox, or button.

Routing in a React application provides flexibility to handle different pages dynamically, allowing for the creation of a platform with multiple applications by rendering components based on routes.

The outlet component is responsible for retrieving the valid page from the parent and mapping components using a switcher to render them based on their type.

The sample app discussed in David's talk can be found on David Villar-Marin's GitHub.

David Villamarin
David Villamarin
12 min
22 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Hey, React Summit! Today I want to share my thoughts on creating a generic React application using TypeScript. I'll discuss the Type Build concept, generic application routing, and provide an example. What is the type build? It is a test that uses the characteristics or properties of an object to determine its type. Duck typing brings flexibility and simplicity, allowing for loose coupling and shorter code. Type guards are used to implement duck typing in TypeScript, enabling custom logic to determine the type of an object. This approach ensures type safety and better error checking. We use a switcher component to render components based on the CMP variable type. When working with React, create components as you would for any application. Use routers for dynamic routes with IDs to handle different pages. The outlet receives the valid page ID from the parents and uses a map to render components based on type. The object result can be seen in the next slide. Create a menu in your application with valid pages. This is an extensible approach for handling complex forms. Find the sample app on my GitHub.

1. Introduction

Short description:

Hey, React Summit! Today I want to share my thoughts on creating a generic React application using TypeScript. I'll discuss the Type Build concept, generic application routing, and provide an example.

Hey, React Summit! My name is David Villar-Marin. Today I want to share with you some of my thoughts about a way to create a generic React application using TypeScript. So first of all, who am I? I'm an Ecuadorian. I'm a developer by day and a chess player by night. I've worked at Stack Builders. That is a consultancy focused on functional programming. We use functional languages, but we also do cool stuff like this. I'm also an open source contributor. You can check my GitHub.

I want to start saying that this is just one of the many ways in which you can create generic applications in React. Just to be aware. So yeah, what do we have today? First of all, we are going to talk about the Type Build. We are going to create a generic application applying this concept, and later on we are going to see some routing of a generic application and also an example. So yeah, let's jump to it.

2. Type Build and Generic React Application

Short description:

What is the type build? It is a test that uses the characteristics or properties of an object to determine its type. Duck typing brings flexibility and simplicity, allowing for loose coupling and shorter code. Type guards are used to implement duck typing in TypeScript, enabling custom logic to determine the type of an object. This approach ensures type safety and better error checking. We start by creating models with a type property, ensuring consistency across components. Then, we create type guards for different component types. Finally, we check the type of the CMP variable to ensure it matches the desired type.

First of all, what is the type build? The type build comes from a test that says basically if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. This is just logical inference that implies that someone can identify a non-subject by observing that subject's characteristics or properties. So we can use this test on the programming world. And yeah, basically we can check some properties and later on guess the type of a certain object.

So why duck typing? This concept brings you flexibility and simplicity so you can have loose coupling code and also for short simpler code. And this encourages better code using, I mean, applying reusability, modularity, and maintainability. So yeah, first of all, how you can implement duck typing on TypeScript using type guards. So what is a type guard? It's a function whose return type is a type predicate. A type predicate is a type assertion that checks if an object has a specific property or set of properties. In this case, we are checking if the pet that could be a fish or a bird has like a stream property. So that allows you to create custom logic to determine whether a value is called upon a specific type. In this case, we are creating this as fish. So we are checking if the pet that could be a fish or a bird, it is indeed a fish. This is a formal type narwhee.

So yeah, this enables type safe code, better compile time and error checking for short. So in this little condition on the left, we are making sure in the first part that pet is always a fish. And since pet could be a fish or a bird on the else statement, we can call period properties or period methods.

So yeah, we are going to start creating this, crafting this generic read application. So first of all, we start with the models. The models are just simple templates. An important part here is the type property. So we are making sure that all our components can have this type property, input, x area, could be a checkbox, could be a button. And also we can have many, many more properties like variant or icon for the buttons or for the accordion. So this is just an example of a container component.

So yeah, after having your models, you can create your type cards. So first of all, you are going to have a CMP variable that is our known object. So could be anything. So yeah, that's why we are creating our type cards. For the text input, for the checkbox, for the button, and also for the accordion. So yeah, we are making sure that after applying these functions, we are checking that this CMP variable is of a certain type.

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
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 2022React Advanced 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.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top ContentPremium
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
A Practical Guide for Migrating to Server Components
React Advanced 2023React Advanced 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.

Workshops on related topic

React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured Workshop
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 2022React Advanced 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.
Build LLM agents in TypeScript with Mastra and Vercel AI SDK
React Advanced 2025React Advanced 2025
145 min
Build LLM agents in TypeScript with Mastra and Vercel AI SDK
Featured WorkshopFree
Eric Burel
Eric Burel
LLMs are not just fancy search engines: they lay the ground for building autonomous and intelligent pieces of software, aka agents.
Companies are investing massively in generative AI infrastructures. To get their money's worth, they need developers that can make the best out of an LLM, and that could be you.
Discover the TypeScript stack for LLM-based development in this 3 hours workshop. Connect to your favorite model with the Vercel AI SDK and turn lines of code into AI agents with Mastra.ai.
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
Workshop
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.