How to setup a typescript project
Typescript is a programming language that is an extension of the JavaScript language. Why? When running JavaScript, you would see errors during runtime. Typescript solves that problem by introducing types, where we can see errors before we even run our program. That is my overview of TypeScript, but I will do an in-depth course later on.
Installing Nodejs
Nodejs is a JavaScript interpreter. In simple terms, in order to run JavaScript we need Nodejs to be installed in our system. You can install Nodejs on the Nodejs website. If you have already installed Nodejs, you can skip this part.
When you install Nodejs, you are also installing npm (Node package manager) automatically. It just comes with Nodejs.
Installing typescript globally
To install typescript globally, we are going to use npm. Open your terminal and run the following command:
Once you have installed typescript globally, you do not need to install it again. Just use it in every project.
Setting up the environment
In order to use TypeScript, we need to setup our node environment first, why? Nodejs is not familiar with TypeScript, so we need to convert the TypeScript to JavaScript. We are going to prove this as we proceed.
Open up a project folder. Open the folder with your code editor of your choice. Then open the terminal, and run the following command:
Proving that Nodejs doesn’t know TypeScript
Create a folder called src and inside that folder create a file called index.ts. Inside the file, write the following code:
Now to run the file we will use Nodejs with the following command:
Converting Typescript to JavaScript
Now that we have our environment setup and we have installed everything we need, let us convert the Typescript to Javascript. In order to convert Typescript to Javascript, use the following command in your terminal:
The problem with the above approach
Every time we make changes to your index.ts we have to convert it to JavaScript manually using tsc src/index.ts. Let us say that we have ten files in the src folder, it will be a pain to manually convert them one-by-one to JavaScript. How do we automate this?
As a developer, time is crucial, hence you would want to automate the easy task and focus on building the complex parts.
The better approach
Instead of manually doing the conversion, let us automate it. Let us start by initializing a Typescript configuration file. Why? This will help us with setting up our typescript environment, like where we want to store our converted files and how we want them to be stored, etc. To initialize this typescript config file, run the following command in your terminal:
Now that we have configured our Typescript environment, Let's work on automating the conversion. Remember that package.json file? This is where it will come in handy. Update only this part of the file:
Running the automation
Create a folder called dist (where we will store our converted JavaScript). Make sure you have all your Typescript files in the src folder. Delete any JavaScript that you have in the src folder. Open your terminal and run the following command:
Conclusion
Typescript is a great language to use because it ensures that we run our programs error-free with no stress. Knowing how to set it up the right way can be challenging, but I hope I helped you. Please share this content with others if you found it useful, or add a comment if you have any questions or suggestions. Happy coding 😄.