0

There are times when you need to work with packages which aren't built with typescript in mind. For example, I'm working with two.js package in a react + typescript project with strict mode turned on. I'm finding it very difficult to type each variable properly as I have no clue what a lot of functions return.

If I use any or don't mention a type, I get annoying errors like variable implicitly has an 'any' type and it feels like I'm in constant battle with the compiler instead of focusing on my work.

A lot of other languages have some function/keyword which gives you the exact class/type an object is based on. But in javascript's typeof & instanceof are completely useless as you can only use them to check if an object is of a certain class. And typeof simply returns object for any custom object.

I love typescript but this is very annoying. How do I get around this? I've tried googling online but couldn't find a solid way to deal with this problem.

Amol Borkar
  • 2,321
  • 7
  • 32
  • 63

1 Answers1

0

If I use any or don't mention a type, I get annoying errors like variable implicitly has an 'any' type

The message explains that the variable in question has no explicit annotation. This won't happen if you explicitly annotate/assert any.


If you want to keep your program type-safe, your only options are to

There's not another way for TypeScript to know what the imported types are because TS is a static, structural type checker, and its purpose is to help keep your program type-safe.

jsejcksn
  • 27,667
  • 4
  • 38
  • 62