Member-only story
How to Change All JavaScript Files to TypeScript
A simple terminal script to change all your .js to .ts files.
If you’re migrating your JavaScript (JS) project to TypeScript (TS), you probably already have tens or hundreds of files that you need to rename to have a .ts
extension instead of .js
.
I had to do this for my project yesterday, and it saved me countless hours give that my project has nearly a thousand files, not excluding node_modules
.
The following script can be ran in root of your project or in any folder of your choice to change your JS files to TS files:
find myProject/src -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.ts"' {} \;
The simple script above was adapted from this gist. All it does is recursively look in your specified folder and its descendant folders for .js
files and changes their extension to .ts
.
So if your project’s root folder is myProject
and you have another folder in there called src
, which holds all of your JavaScript files, you can target that folder and its descendants by specifying myProject/src
as the target file in the script.
Once you run that in your project folder through your computer’s terminal, you’ll see that all of your previously .js
files are now .ts
files. You've successfully changed your JavaScript files to TypeScript files in a single batch.
With all the time you’ve saved, you can proceed with actually defining your types, interfaces, and enums.
Happy coding!