Creating a TypeScript Project
Suppose we want to build a Node.js application using TypeScript.
Step 1: Create a project folder
Step 2: Initialize npm
This creates a package.json file
Step 3: Install TypeScript
Here:
npm installinstalls a package.-Dmeans development dependency.- TypeScript is needed during development and compilation.
Step 4: Generate tsconfig.json
This creates the TypeScript compiler configuration file.
Step 5: Create your source folder
Inside src, create app.ts.
and write this code.
Step 6: Compile TypeScript
This compiles your TypeScript files according to tsconfig.json.
Step 7: Execute JavaScript
What is package.json?
package.json is the main configuration and metadata file of an npm project.
Let's understand its properties.
Property | Purpose |
|---|---|
| Project name |
| Project version |
| Project information |
| Package entry point |
| Custom commands |
| Packages required at runtime |
| Packages required during development |
C. What are npm scripts?
This is particularly useful.
Instead of repeatedly writing:
You can define a script inside package.json:
Now execute:
And:
there are some predefined commands.
Command | Explanation |
|---|---|
|
|
|
|
|
|
| Restart lifecycle |
|
|
|
|
Now install: