How to handle notImplictAny during migration from js to ts without time wasting
When you are trying to migrate from JavaScript to TypeScript you will have an error noImplicitAny when you variable or parameter will not have assigned type.
There are to two bad solutions
Quick and dirty
To avoid that options in quick and dirty way you can:
- Disable
noImplicitAnyintsconfig.json, but I don’t recommend it to you because- You are migrating to typescript to have types. Am I right?
- You will have to do this in the future.
- Use
anyon any place where type is required, but:- There is no value for typescript if you use
anyeverywhere - You will be not able to distinguish:
- where
anyis used consciously - where
anyis used, just to build the application
- where
- There is no value for typescript if you use
Slow and clean
- Create a type for every variable and parameter, but:
- It will take you a lot of time
- It is not required if you don’t have cooperate with that code
- You will be tired, because it is very boring job
The best solution to avoid noImplicitAny error
- Create a file
fakeAny.tswith content:export type fakeAny = any - In any place where you have
noImplicitAnyusefakeAnyinstead odany - Then your application will be build and you will have a huge amount of benefits
Benefits of using fakeĄny instead of any
- You can continue your work almost immediately.
- You know where you want to use
anyand where it is only temporary placeholder for TS compiler - You can find all
fakeAnyoccurrences easily. So you will know what parts of the code are not migrated to TypeScript completely.- Or you can remove
fakeAny.tsto check how many errors you have. It is useful when you are on the end of migrating.
- Or you can remove
A small request to you :)
If you think you will use it or you have different approach for the same problem. Let me know and leave a comment.
Thank you
