Annotations and Assertions 12 exercises

The @ts-nocheck Directive in TypeScript

There's one final way to completely remove type-checking from a file if you want to. At the very top of a file, you can add the following:

// @ts-nocheck

Using @ts-nocheck tells TypeScript not to check the file and not to throw errors within the file. It's a free-for-all, and y

Loading explainer


00:00 There is one final way that you can completely remove type-checking from a file if you want to. You can, at the very top of a file, just say ts no-check. And ts no-check will say to TypeScript, do not check this file. Do not throw errors within this file.

00:17 And so it's a free-for-all. You can do whatever you like. Inside our handleFormData, you can say e.prevent if you want to. You don't have to say prevent default. You can say da instead of data. You can say obj instead of object. You will not get any errors from this. Apart from, all of your runtime will break.

00:35 So, and of course, if we remove ts no-check, then we get all of these errors back. Ts no-check, I think, has actually cost me a fair amount of hours in my life because, you know, when you go into a file, you don't expect a ts no-check to be at the top. And you'll go, why is TypeScript just not working in this file? What happened to my developer experience?

00:53 And you'll just find a little ts no-check at the top. I don't think I can recommend this at all. I don't think there are any situations in which you need this. Apart from, let's say, if you're in a migration process maybe

01:12 and you're migrating a load of files over to TypeScript and you want to just only convert certain files over. But then again, I think that might just be better just to have those files in JS instead. Because at least that's an indicator that, you know, TypeScript should never work in these files.

01:29 I don't think ts no-check has a place in any application code base unless it's a legacy thing and you slowly want to be getting rid of it. So ts no-check is certainly a tool that is available but I would shudder if I ever saw it in an application code base again.