تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Never Type Exhaustiveness0:59

loop, so a while true that never sets an exit condition. These kind of functions never return. Throwing an error doesn't return, so that handles the Never return type. That's pretty straightforward. But what do we mean by a function that takes a parameter of type Never? Nothing can ever be assigned a type Never, so how does this work? And the thing to realize is it doesn't work, and that's the beauty of it. This can never actually be called without throwing an error, so this really helps us do exhaustiveness checking for discriminated unions. So let's take our UserEvent that we had in our last lesson, and this time, let's add

Adding UserUpdated Event1:27

do exhaustiveness checking for discriminated unions. So let's take our UserEvent that we had in our last lesson, and this time, let's add a UserUpdated event. So let's create a new interface for it here. It's going to have the event type UserUpdated, and it's just going to have some more parameters. So now we're just going to update our better UserEvent to also have a UserUpdated event. So now our event is the union of these three types, and we're going to take our exact same handler again. It's now going to have all three of these types in the union. Let me just comment this out quickly just to show you something.

Using unreachableCaseError1:54

It's now going to have all three of these types in the union. Let me just comment this out quickly just to show you something. So while our BetterUserEvent is just the created and deleted, this handler is perfectly fine. It doesn't have any type errors anywhere. We check if it's a created event, we handle it, and we return. We check if it's a deleted event, we handle it and return. Then we call this unreachableCaseError, which is this little utility we created here with the never type. And we pass it the event in, but the event is a BetterUserEvent, but this function

Type Error Flags Missing Case3:10

and there's a ticket somewhere to extend the BetterUserEvent with another union type. So in our case, the UserUpdatedEvent, let's just union that correctly. And doesn't see this handler in the code for whatever reason, immediately, we're going to get a type error here. So we're going to say argument of UserUpdatedEvent is not assignable to parameter of type never. So this is a really handy error message, just basically telling us that we haven't handled our case of UserUpdatedEvent yet. And this is very, very useful because it picks up not runtime behavior, because if this wasn't here, we would get no runtime error, but it's a potential logic error because we have an

Fixing With Added Handler4:16

And I pretty much just copy and paste this utility into my projects and put this at the bottom of every, every check. So to get rid of this now, all we need to do is user just needs to come along and add a handler for user updated. And then we are, I call this param. So let's call it parameters just to be consistent. And there we go. Now that error has disappeared. Again, if we comment this out, we accidentally delete this for some reason, or we add a new condition, we get a static type failure, which means we don't have to run this code, write.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟