Type Predicates Solve This Common TypeScript Error

preview_player
Показать описание

Type predicates are an interesting feature in TypeScript since they cover a very niche use case that can cause type errors if you are not aware of this solution.

📚 Materials/References:

🌎 Find Me Here:

⏱️ Timestamps:

00:00 - Introduction
00:17 - The Problem
02:12 - Type Predicate Basics
03:15 - Type Predicate Problems

#TypeScript #WDS #TypePredicate
Рекомендации по теме
Комментарии
Автор

In all my TS project I add this utility function:

function isDefined<T>(value?: T) value is T {
return value !== null && value !== undefined;
}

It's very useful when I have a list of items where some of those items might be undefined:

=> {
// `item` is now correctly inferred to not be undefined or null
})

bvx
Автор

I happened to run into this very same issue just a week ago, and learned what you're teaching, and made great use of it.
Now you teach it with a sweet simple example, further cementing my knowledge of the subject.

Great job man!

gameraz
Автор

Afaik, this is actually called a type guard. A very important notion indeed 😊

Niksorus
Автор

Your video about the type predicates is more worth thousand times than the official document.

ThaiNguyen-ggxj
Автор

The TS website doesn't do a good job explaining this, but you did! Thanks!

benjamin-lieb
Автор

This man really simplifies everything in web dev. Great content. Just love it. ❤️

kaushikchopra
Автор

This is excellent! Thankyou! I didn’t know about this one ❤

jx
Автор

User-defined type assertion functions using `asserts` would be a great addition to this video, since it can build upon this predicate function.

_nikeee
Автор

Thanks, man, very interesting as usual.

anibalsancheznuma
Автор

That's a really cool feature but the OOP dev inside me want's to scream 😂

macigli
Автор

Brilliant. I've been wondering about this!

Duckception
Автор

I dont understand. isEmployee() returns a boolean. So I would write: isEmployee(person:User| Employee): boolean. Where do I write the boolean return type in your example ?

benjaminjeschke
Автор

How about narrowing using switch case ? how should I return the exact type for multiple cases ?

ryzott
Автор

I think object design patterns also play a role here. The shape of the data object should be consistent for all persons. If the person is merely a User then he/she should still have an email key of "null". Then assign NonNullable to Employee's email type.

I don't like using "as" or "is" as it could cause unintended bugs like shown in the video.

Thassalocracy
Автор

I'd love to see some videos on Deno's fullstack Fresh framework! I think it's a great alternative to Node.js frameworks such as React and Next.js.

mmanomad
Автор

With TS 5.5 we'll no longer need to explicitly add `:person is Employee` 🚀

Szarlus
Автор

Handy when you have nested union types

snakeb
Автор

Isn't this kind of getting into runtime type checking? For that I prefer to just use Zod.

JediMediator
Автор

why not an enum with user and employee?

ヤッシン_yssin
Автор

You can make the argument type only User btw

nomadshiba