Transcript from the "ts-ignore" Lesson
>> So, TypeScript 3.9 gave us this, @ts-expect-error. This is closely related to @ts-ignore. So if I had something like this, Both of these would effectively suppress the error that we should be seeing here, right? So if I get rid of my @ts-ignore I'm gonna get yelled at, because a string is not a number.
[00:00:37] And same goes for this, right? I'm gonna get yelled at here. You can think of @ts-expect-error, like I think of it as, in almost every place, a better option than @ts-ignore, and here's why. And let me make these different so we don't, we eliminate this re-declaration aspect of things, right?
[00:01:03] So they're independent now, but if I were to select both of these and change it to the number 5, we can see that @ts-expect-error is now unhappy with me, @ts-ignore is fine. And here's why. It's an unused directive, I'm told. And what that means is I said, this should error, and I want to ignore that error.
[00:01:26] @ts-ignore it is more like saying, if any kind of error happens here or maybe it doesn't, then ignore that. So this brings to my attention if this bypass is no longer necessary, and that could very well be an indication in my code that something is wrong. Like if I add a @ts-ignore, there's a reason for it, and I want to be alerted if it's no longer necessary, because that could mean that there's some other change, right?
[00:01:55] Let's say that this happened. What if we did this? Let me go back here, and let's say that we're not dealing with number we're dealing with some other type. And we went from, Let's pretend that this is in another file, Right? Well, let me change it. Guess I'm using food too many times.
[00:02:20] There we go. So let's say that this is in another file and I change it from a food to an any. Well, that's a problem. I might have just like lost some type information. Maybe there's somewhere somebody has done this, like number and any, right? And when you, and when you say anything that is a number or also anything else, it could be anything else in addition to being a number.
[00:02:45] Well, that's just anything, right? So I've effectively made bar and any. That's a bug. This type should not be so permissive, and now it is brought to my attention. So I like places where, when I am saying I want to surgically bypass type checking here, maybe it's a negative test case.
[00:03:07] It's something that shouldn't work. It had better not work. If it works, there's a problem. Well, now I have an assertion. Now I have a mechanism for being made aware of this, whereas @ts-ignore, it doesn't give me any sign. I basically just turned TypeScript off there, instead of saying, there should be an error here and ignore that error.
[00:03:30] So I use @ts-expect-error almost every place that I would have been using @ts-ignore. It's a bigger deal than it may seem.