Ugly TypeScript Code Is Actually Good (Sometimes)

Web Dev Simplified · Intermediate ·💻 AI-Assisted Coding ·1y ago

Key Takeaways

The video discusses the use of advanced TypeScript features such as the extends keyword, infer, and nested turnaries to create complex types, and how these features can make code more difficult to read but provide type safety and ease of use for library users.

Full Transcript

I recently released an advanced TypeScript video talking all about like the extends keyword, how you can use infer, as well as quite a few other things to create really complicated TypeScript types. And I received a ton of comments saying code that looks like this is incredibly complex, overengineered, and just bad code in general. And I think that those people are both right and wrong. And I want to talk a little bit about why that is. First of all, yes, this code is very difficult to read, especially with how zoomed in my screen is. If I zoom it out to a more reasonable screen size, you can see it's a little bit easier to read. But overall, it's still incredibly complicated code with lots of nested turnaries and so on. And that's a little bit of a side effect of TypeScript. When you start to write more complicated code, the only way to do an if statement inside of TypeScript is a turnary. So you end up with tons and tons of nested turnaries, which gets rather difficult to read unless you're familiar with TypeScript and how they actually use these turnaries for if statements. And generally when you're writing applications with TypeScript, your code is going to look something more like this. You're going to have a basic type. You're going to have something that uses that type. And that's pretty much all the TypeScript in your code. So 99% of the time you're going to be working with very simple types with for example very basic unions. But as soon as you start writing code for libraries that's when you need to start using these advanced TypeScript features. And the reason I needed all these features is because for example I was creating a translation app and I wanted to be able to translate for example what all my translation keys are. So I can see all my different translation keys. I also can give myself errors saying hey I actually require a second property on here that requires me to pass in a bunch of additional information. For example my name and so on. So I need all these additional TypeScriptbased features that I wouldn't get by just doing this purely inside of JavaScript. Same thing here. I get an exact perfect TypeScript representation of the only two values that I can pass along as the hobby property. These TypeScript types are actually so powerful that when I actually create a type down here, for example, I can specify exactly the parameters of what I want and it gives me full type safety for that. For example, here I said a hobby is an enum. And now inside my enum I have a key of hobby. And if I remove this, I'm going to get a TypeScript error. And I'm going to need to pass in hobby, which I have TypeScript safety for. And I need to pass in specific values for that, like I did here. And if I go ahead and I rename this to hobby 2, I'm going to get an error cuz this must be called hobby 2. So again, all of that extra TypeScript code that I write is implementing these features so that when you use this library, it's very easy for you. And as you can see, I didn't have to write any TypeScript inside of here to write my actual code because the library took care of all of that behind the scenes for me. And this is something you'll see in almost every TypeScript library. For example, I copied down the types for a very popular TypeScript library for internationalization. And as you can see, the types inside here are very similar to what I showed my application. We have lots of extends, we have lots of infers, lots of complicated nested turnary statements that are very difficult to work with and read. But that's just the reality of writing complex TypeScript for libraries. Usually the easier to use a library is, the more complex the types are behind the scenes. And sometimes in cases of like my application here, I actually have one to one TypeScript code to JavaScript code. So for every line of JavaScript code, I have a line of TypeScript code that is making that work behind the scenes to make it really easy for you to use that in your own application.

Original Description

Full Video: https://youtu.be/bnTAOB3P6nM 🌎 Find Me Here: My Blog: https://blog.webdevsimplified.com My Courses: https://courses.webdevsimplified.com Patreon: https://www.patreon.com/WebDevSimplified Twitter: https://twitter.com/DevSimplified Discord: https://discord.gg/7StTjnR GitHub: https://github.com/WebDevSimplified CodePen: https://codepen.io/WebDevSimplified #Shorts
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Web Dev Simplified · Web Dev Simplified · 0 of 60

← Previous Next →
1 Introduction to Web Development || Setup || Part 1
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
2 Introduction to Web Development || Understanding the Web || Part 2
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
3 Introduction to HTML || Your First Web Page || Part 1
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
4 Introduction to HTML || Basic HTML Elements || Part 2
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
5 Introduction to HTML || Advanced HTML Elements || Part 3
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
6 Introduction to HTML || Links and Inputs || Part 4
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
7 Learn Git in 20 Minutes
Learn Git in 20 Minutes
Web Dev Simplified
8 5 Must Know Sites For Web Developers
5 Must Know Sites For Web Developers
Web Dev Simplified
9 10 Best Visual Studio Code Extensions
10 Best Visual Studio Code Extensions
Web Dev Simplified
10 Learn CSS in 20 Minutes
Learn CSS in 20 Minutes
Web Dev Simplified
11 How to Style a Modern Website (Part One)
How to Style a Modern Website (Part One)
Web Dev Simplified
12 How to Style a Modern Website (Part Two)
How to Style a Modern Website (Part Two)
Web Dev Simplified
13 3D Flip Button Tutorial
3D Flip Button Tutorial
Web Dev Simplified
14 How to Style a Modern Website (Part Three)
How to Style a Modern Website (Part Three)
Web Dev Simplified
15 Animated Loading Spinner Tutorial
Animated Loading Spinner Tutorial
Web Dev Simplified
16 How to Write the Perfect Developer Resume
How to Write the Perfect Developer Resume
Web Dev Simplified
17 Animated Text Reveal Tutorial
Animated Text Reveal Tutorial
Web Dev Simplified
18 Learn Flexbox in 15 Minutes
Learn Flexbox in 15 Minutes
Web Dev Simplified
19 Custom Checkbox Tutorial
Custom Checkbox Tutorial
Web Dev Simplified
20 Start Contributing to Open Source (Hacktoberfest)
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
21 JavaScript Shopping Cart Tutorial for Beginners
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
22 Responsive Video Background Tutorial
Responsive Video Background Tutorial
Web Dev Simplified
23 1,000 Subscriber Giveaway
1,000 Subscriber Giveaway
Web Dev Simplified
24 How To Prevent The Most Common Cross Site Scripting Attack
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
25 Transparent Login Form Tutorial
Transparent Login Form Tutorial
Web Dev Simplified
26 The Forgotten CSS Position
The Forgotten CSS Position
Web Dev Simplified
27 How to Code a Card Matching Game
How to Code a Card Matching Game
Web Dev Simplified
28 10 Must Install Visual Studio Code Extensions
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
29 Learn CSS Grid in 20 Minutes
Learn CSS Grid in 20 Minutes
Web Dev Simplified
30 Learn JSON in 10 Minutes
Learn JSON in 10 Minutes
Web Dev Simplified
31 10 Essential Keyboard Shortcuts For Programmers
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
32 What Is The Fastest Way To Load JavaScript
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
33 Differences Between Var, Let, and Const
Differences Between Var, Let, and Const
Web Dev Simplified
34 How To Install MySQL (Server and Workbench)
How To Install MySQL (Server and Workbench)
Web Dev Simplified
35 Learn SQL In 60 Minutes
Learn SQL In 60 Minutes
Web Dev Simplified
36 How To Solve SQL Problems
How To Solve SQL Problems
Web Dev Simplified
37 What Are Design Patterns?
What Are Design Patterns?
Web Dev Simplified
38 Null Object Pattern - Design Patterns
Null Object Pattern - Design Patterns
Web Dev Simplified
39 Your First Node.js Web Server
Your First Node.js Web Server
Web Dev Simplified
40 How To Setup Payments With Node.js And Stripe
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
41 How To Learn Any New Programming Skill Fast
How To Learn Any New Programming Skill Fast
Web Dev Simplified
42 Asynchronous Vs Synchronous Programming
Asynchronous Vs Synchronous Programming
Web Dev Simplified
43 JavaScript ES6 Arrow Functions Tutorial
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
44 Are You Too Old To Learn Programming?
Are You Too Old To Learn Programming?
Web Dev Simplified
45 JavaScript Cookies vs Local Storage vs Session Storage
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
46 JavaScript Promises In 10 Minutes
JavaScript Promises In 10 Minutes
Web Dev Simplified
47 Builder Pattern - Design Patterns
Builder Pattern - Design Patterns
Web Dev Simplified
48 JavaScript == VS ===
JavaScript == VS ===
Web Dev Simplified
49 JavaScript ES6 Modules
JavaScript ES6 Modules
Web Dev Simplified
50 8 Must Know JavaScript Array Methods
8 Must Know JavaScript Array Methods
Web Dev Simplified
51 CSS Variables Tutorial
CSS Variables Tutorial
Web Dev Simplified
52 JavaScript Async Await
JavaScript Async Await
Web Dev Simplified
53 How To Choose Your First Programming Language
How To Choose Your First Programming Language
Web Dev Simplified
54 Easiest Way To Work With Web Fonts
Easiest Way To Work With Web Fonts
Web Dev Simplified
55 Singleton Pattern - Design Patterns
Singleton Pattern - Design Patterns
Web Dev Simplified
56 Responsive Navbar Tutorial
Responsive Navbar Tutorial
Web Dev Simplified
57 CSS Progress Bar Tutorial
CSS Progress Bar Tutorial
Web Dev Simplified
58 Learn GraphQL In 40 Minutes
Learn GraphQL In 40 Minutes
Web Dev Simplified
59 What is an API?
What is an API?
Web Dev Simplified
60 Learn How To Build A Website In 1 Hour!
Learn How To Build A Website In 1 Hour!
Web Dev Simplified

The video explains how advanced TypeScript features can be used to create complex types and provide type safety, making it easier for users to work with libraries, but also making the code more difficult to read. It highlights the importance of understanding and using these features effectively in library development.

Key Takeaways
  1. Use the extends keyword to create complex types
  2. Utilize infer to create conditional types
  3. Implement nested turnaries to handle complex logic
  4. Create reusable libraries with advanced TypeScript features
  5. Test and review code for readability and maintainability
💡 Advanced TypeScript features can provide type safety and ease of use for library users, but require careful consideration and implementation to avoid making the code too complex and difficult to read.

Related Reads

📰
Schema vs prompting: how Be Recommended by Inithouse thinks about getting cited by AI
Learn how Inithouse approaches getting cited by AI, comparing schema vs prompting methods, to improve AI engine scores
Dev.to · Jakub
📰
I made Donald Trump an AI World Cup commentator for USA vs Belgium. Here’s how it went
Learn how to create an AI commentator using Donald Trump's voice and style for a World Cup match, and understand the possibilities of AI in sports commentary
Reddit r/artificial
📰
Indian AI coding startup Emergent becomes a unicorn with $130M Series C
Emergent, an AI coding startup, reaches unicorn status with $130M Series C funding, achieving $120 million annualized revenue run rate and 200,000 paying customers, demonstrating the growing demand for AI-powered coding solutions
TechCrunch AI
📰
Disposable software
Learn how the rise of AI-powered coding tools like Claude Code is changing the way we think about software development and maintenance
Seth Godin's Blog
Up next
How to Create ONE PAGE Website using Claude AI (FREE & FAST)
Quick Tips - Web Desiign & Ai Tools
Watch →