Mastering advanced concepts in TypeScript

Rate this content
Bookmark

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.

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

FAQ

Using TypeScript in a web group can make your codebase more reliable and maintainable, which may attract skilled developers who appreciate working with typed languages. You can start by integrating TypeScript into your existing projects and showcasing this in your job postings to attract TypeScript developers.

To use TypeScript in a Visual Studio Code project, you need to install TypeScript and configure it in your project. You can do this by running `npm install typescript` and creating a `tsconfig.json` file in your project root. Visual Studio Code will automatically detect the TypeScript configuration and provide you with IntelliSense and other TypeScript features.

Some advanced concepts of TypeScript include union types, type guards, recursive types, template literal types, and distributive conditional types. These concepts help in creating more precise type definitions and catching bugs early in the development process.

Using strict types in TypeScript helps you catch bugs early in the development process, provides better IntelliSense support, and makes your code more robust and easier to maintain. Strict types enforce stricter type checking, which can prevent many common programming errors.

TypeScript can help prevent bugs by enforcing type checks at compile time. This ensures that variables and functions are used correctly according to their defined types. TypeScript also provides features like union types and type guards, which help catch potential issues early.

For developing a TypeScript project, you need a code editor with TypeScript support, such as Visual Studio Code. You also need Node.js and npm (Node Package Manager) to install TypeScript and other dependencies. A TypeScript configuration file (`tsconfig.json`) is required to set up your project.

Some developers view types as obstacles because they can initially seem restrictive and require more upfront effort to define. However, types provide significant long-term benefits by catching errors early and improving code readability and maintainability. Understanding these benefits can change the perception of types from obstacles to helpful tools.

TypeScript helps explore APIs by providing IntelliSense support in code editors like Visual Studio Code. When you type code, TypeScript can offer suggestions and auto-completions based on the types defined in the API, making it easier to understand how to use the API correctly.

A type guard in TypeScript is a function that determines if a variable belongs to a specific type. Type guards are used to perform runtime checks that provide TypeScript with type information, enabling more precise type checking and reducing the risk of runtime errors.

In TypeScript, optional properties can be handled using union types and type guards. You can define a type that includes optional properties by using the `?` operator. Type guards can then check whether these properties are present before using them, ensuring that your code can safely handle cases where the properties are undefined.

Jiri Lojda
Jiri Lojda
132 min
06 Nov, 2023

Comments

Sign in or register to post your comment.
  • Parmar Tarun
    Parmar Tarun
    SUNY Binghamton
    Hi, when can we have the recording available for this workshop? Thanks
  • Scott Huffman
    Scott Huffman
    Fidelity Investments
    Not sure if anyone is monitoring these comments for questions--my company has disabled access to zoom's chat--but I wanted to ask about this KeyToGuard, and whether there is any ways to break it into smaller, different types to improve readability?

Video Summary and Transcription

The Workshop covers advanced concepts in TypeScript, such as strict types and type guards. It explores the use of development tools and the advantages of using TypeScript in software development. The Workshop also delves into mapping union types and object types, as well as keywords and conditional types. Bug fixes and type validation are discussed, along with recursive parsing and entity validation. The Workshop concludes with a final bug fix and key takeaways for using TypeScript effectively.

1. Introduction to TypeScript and its Advantages

Short description:

I'll show you some advanced concepts from TypeScript to make you more comfortable using types. We'll discuss advantages of using strict types, work on examples with bugs, and explore the preciseness of types in APIs.

How to use TypeScript to hire employees in a web group How to use TypeScript in a VSC project How to alors create .sush I'd like to show you some a little bit advanced concepts from TypeScript just to clarify some things and maybe make you more comfortable using these types and see the value in them.

Today I'm here with Andrey from Content.ai. He'll help me manage the questions and chat. So if you've got any questions, feel free to ask. And ok, yep, that's me, you can contact me for example on GitHub you can see some things I work on. at the at content by a company profiling it up as well and okay I was gonna be on the other agenda we're gonna start by discussing some advantages of using the strict strict types like the strictest times that are available for the situation.

Next we'll work on the first example where there will be a small bug in an application that I've prepared and we'll talk about how better typing or better use of the Typekit type system would prevent such a bug and during that we'll uh we'll try out some of the some of the typeset concepts then we'll go on to another example and then depending on the time and maybe our energy we we can try the bonus that the bonus stuff that i've prepared that's a little bit more advanced more about playing with typescript and stuff like that so we'll see if if you are interested in that so at first uh i'll show you the application that we'll be working on it's just a simple events application that manages some events you've got you've got events maybe just a little bit i think i've got the buggy version right now running So, I'll run the other one.

Yeah, the application itself. You can maybe just to stop that you can download it from this URL. I'll copy paste it into the chat so that you don't have to find it look for it in the on the screen it's all the other how great thanks so thank you for that and now the application should run smoothly and as I said it's a very simple application that manages some events, you can click on the event, there's a description, their names, I can create a new event, I can select an organizer and it's there. I can delete events, I can create events for a certain organizer right from here, and I can copy events. So very simple. And yeah. So let's continue with this.

So, why would we even want to bother with with types and type system. The thing is, I often see that people think about types as their enemies, something they have to combat with and there are always obstacles in their way. But to do this, type system is there to help you. It can find a, it can catch bugs. It can really catch a lot of bugs And it's there to have your back in case you do some typos and mistakes, this is very often. And can also help you explore APIs. Like, often if I start with a new library, I just type some things, some function names and stuff like that, and I look for types, what the IntelliSense, for example, offers me, in my code editor.

For example, if I open this example file, you can see a function, write by reference that accepts an object, a reference, that has two optional properties, ID and external ID. And now from this API, I say okay, I'm going to call the function, and what should I provide the function? Well, I have no idea. Should I call it with an empty object? TypeSketch is fine with that. The TypeSketch is not able to advise me because according to TypeSketch, this is fine. But it probably isn't fine because the reference is just an empty object. There's nothing. Probably what the author really intended to do is to say, okay I want you to provide either an ID or an external ID. That's probably what the author meant but he didn't provide me any exact guidelines for that. So I'm lost here. But if he instead did something like this like this, okay, I want a union type where I want the reference to be an object with one property id that has to be there, or another, or a a different object that instead has a property external ID, that's not optional and always has to be there, now, the type key tells me, okay. MT object is not a valid parameter for this function. You either need to provide an object with ID property or external ID property, but either one has to be there. And at the same time, if I say... This is fine. That's disappointing. I thought he was going to tell me that I can't provide both of them, but well, that's not the case. But at least I know I have to provide one of them. So in this way, TypeScript really helps me to explore the API and see what's available and what I have to or don't have to provide. It's really much more clear even if you read the type, you see one of them is available. Or one of them is necessary. uh regarding the preciseness of types uh you can you can also often see a function like this a function takes some string that's uh that uh has to repre that represents some kind of action you want to take for in this example uh you want you want to create an event of a certain type and then you switch on the type and you provide different kind of event for each type. And then if you receive an invalid, some kind of unknown event type, you just throw an error because that's an invalid state for you, right? And then as a user of such an API, I'm going to go and say, okay, I'm going to call this event type and I have to provide a string. Okay, what kind of string? I don't know. I'm just going to slap there some string. and it's going to throw an error because this is neither of these strings that are used by the function. Again, if the author of the API would instead write something like this... and now, I've got an error. and the typeskip tells me, okay, you have to provide this or that. Pretty clear.

QnA

Watch more workshops on 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.
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. 
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
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.

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

Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
This Talk discusses modern web debugging and the latest updates in Chrome DevTools. It highlights new features that help pinpoint issues quicker, improved file visibility and source mapping, and ignoring and configuring files. The Breakpoints panel in DevTools has been redesigned for easier access and management. The Talk also covers the challenges of debugging with source maps and the efforts to standardize the source map format. Lastly, it provides tips for improving productivity with DevTools and emphasizes the importance of reporting bugs and using source maps for debugging production code.
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.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
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.