Setup a TypeScript Project Fast
Create a New Project in 30 seconds!
Learn how to install TypeScript and run your first file!
A lot of people think getting started with TypeScript is scary at first, especially when you're used to JavaScript.
This article will guide you through setting up a TypeScript project, allowing you to start reaping these benefits immediately and testing out TypeScript today.
1) Initialize Your Project
Let's create a new directory for our project and initialize it with npm:
mkdir typescript-project cd typescript-project npm init -y
Note: The -y
flag (on npm init) accepts all the default setup options for the npm project setup.
2) Install TypeScript
We can add TypeScript as a development dependency to our project by using the following command:
npm install typescript --save-dev
3) Create a TypeScript Configuration File
You need a special configuration file named tsconfig.json
file in a directory that indicates that the directory is the root of a TypeScript project.
We can generate it easily with some good defaults with the following command:
npx tsc --init
Create your first TS file
You can start writing TypeScript!
Create a new file in your project called index.ts
(don't forget the extension is ts
for TypeScript).
Optional: Run your script
To run your script, let's add one more package called ts-node
:
npm install ts-node --save-dev
Now that it's installed, we can run the following command to run our index.ts
:
node --loader ts-node/esm index.ts
Optional: Compiling your TS
You can compile your TypeScript to JavaScript by running the following in your root folder:
npx tsc
If you want to compile your files automatically, run the command with the --watch
flag. Here's the full command:
npx tsc --watch
Follow me on Twitter or connect on LinkedIn.
🚨 Want to make friends and learn from peers? You can join our free web developer community here. 🎉