Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set tsconfig to ignore "Property 'foo' does not exist on type 'bar'." but only for a specific variable

I have an object foo that is used all around the codebase and need to access the property bar from it. However, due to specific circumstances ts thinks that property does not exist on it, even though it in fact does. So I am getting the redline with this message all over the place:

Property 'foo' does not exist on type 'bar'

In my ts-config file, I would like to set it to ignore all these errors, but ONLY for the foo variable.

Can this be done?

like image 815
theJuls Avatar asked Oct 30 '25 07:10

theJuls


1 Answers

You can't do this in tsconfig, there is no such setting. You can do one of several things.

Use a type assertion to any. Using this all property accesses are allowed:

(bar as any).foo

Another option is to just suppress the error. You can use @ts-ignore to suppress ALL errors for the next line

//@ts-ignore
bar.foo // no error

Playground Link

Note: You might be better off finding the reason for the error, or augmenting the type or charging the type of the variable to something that incudes foo but without more details it is difficult to propose a solution.

like image 117
Titian Cernicova-Dragomir Avatar answered Nov 01 '25 21:11

Titian Cernicova-Dragomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!