This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
How do you deal with TypeScript when (for whatever reason) types are mixed up, everything's full of squigglies, but you're sure you want to ignore this one particular TypeScript error?
Let's look at a very simplified example: say you have a method taking in a string
and returning a string
.
ts
functiongreet (name : string): string {return `Hello, ${name }!`;}
You want to use the method with a number
type, though. Of course, TypeScript isn't happy about how you use greet
, but you're 100% certain you want and need to pass a number
.
ts
Argument of type 'number' is not assignable to parameter of type 'string'.2345Argument of type 'number' is not assignable to parameter of type 'string'.greet (123 );
What do you do?
You can bring in your friends @ts-ignore
and @ts-expect-error
to silence TypeScript.
ts
// @ts-ignoregreet (123);// @ts-expect-errorgreet (123);
Both, @ts-ignore
and @ts-expect-error
, will make TypeScript ignore the type error happening on the following line. But which one should you use?
After doing some reading, generally, it's recommended to go for @ts-expect-error
because it will lead to a TypeScript error when the following line isn't erroring anymore.
ts
// @ts-ignoregreet ('Stefan');Unused '@ts-expect-error' directive.2578Unused '@ts-expect-error' directive.// @ts-expect-error greet ('Stefan');
The difference between the two comment directives is that @ts-expect-error
forces you to clean up later. @ts-expect-error
will let you know that you can remove it whenever the issue you faced is resolved. @ts-ignore
will stay put and silently sit wherever you placed it.
But whatever you choose, maybe it's news to you that you can extend these @ts
-comments as well. So, when you place @ts-
comments in your codebase, do yourself a favor and document why they're there.
ts
// @ts-ignore: I'm sure this should be a number.greet (123);// @ts-expect-error: I'm sure this should be a number.greet (123);
That's it for today. ✌️
Reply to Stefan
This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Stefan Judis | Sciencx (2025-02-05T23:00:00+00:00) The difference between @ts-ignore and @ts-expect-error (#tilPost). Retrieved from https://www.scien.cx/2025/02/05/the-difference-between-ts-ignore-and-ts-expect-error-tilpost/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.