dev-resources.site
for different kinds of informations.
How to Set Up a New TypeScript Project
Published at
7/5/2024
Categories
typescript
setup
javascript
webdev
Author
asimnp
Author
6 person written this
asimnp
open
TypeScript is JavaScript with syntax for types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. .ts
is the extension of TypeScript file.
1. Setup
- Before installing TypeScript on your system, first you need to install Node.js. You can check the official documentation for installating it.
- Type the following command to check to ensure Node.js is install. The command gives out the Node.js version.
> node --version
- Example:
- Create a new directory and go inside it.
> mkdir code && cd code
- Initialize
package.json
file. Its a file which contains all the packages or dependencies install for the application.
> npm init -y
- Install TypeScript
> npm install typescript --save-dev
-
--save-dev
flag indicate we are installing TypeScript for development purpose and we don't need this package for production. - Initialize TypeScript config file.
> npx tsc --init
2. Set Up Project
- Create new files and folders inside the
code
folder.
> touch index.html
> mkdir src dest
> touch src/main.ts
- Folders and files tree
- NOTE: Browser can only understand JavaScript, so you need to compile the TypeScript into Javascript.
- Specify where to output the compile JavaScript. Customize the
tsconfig.json
file to output inside the.dest
folder.
// tsconfig.json
...
"outDir": "./dest", /* Specify an output folder for all emitted files. */
...
- The
...
three dots indicates their is more code. - Create a script that will watch your TypeScript file changes and automatically compile it into JavaScript. We will create a new script
start
inside thepackage.json
file.
// package.json
{
"name": "code",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "tsc --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"typescript": "^5.5.3"
}
}
- We will add some code inside our TypeScript file
src/main.ts
const username: string = "Jane Doe";
console.log(`Hello, ${username}!`)
- Now, we will run the
start
script to watch the TypeScript file changes.
> npm run start
- The
start
script command will automatically create a new file calledmain.js
inside thedest
folder which is our compiled JavaScript file. - Inside our
index.html
we will link our compiled JavaScript file and open it on the browser. Then, check the console to verify the message is logged.
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TypeScript Project Set Up</title>
</head>
<body>
<h1>Press <code>F12</code> and check your browser console. There you will see the message which we have written.</h1>
<script src="./dest/main.js"></script>
</body>
</html>
- Now, you can add your code and create your project!
setup Article's
30 articles in total
.NET DEVELOPER TOOLS NEED TO INSTALL FOR NEWLY PC
read article
Exploring Localhost 127.0.0.1:49342: Setup and Usage Guide
read article
Mastering Jenkins: A Step-by-Step Guide to Setting Up and Supercharging Your CI/CD Workflow
read article
Personal TODO list on how I set up my dev machine
read article
CĂłmo usar varias SSH keys para diferentes repositorios de GitHub (o GitLab)
read article
Tech and Tools I use
read article
How to Set Up a New TypeScript Project
currently reading
How to install SDL2 on macOS
read article
Multiple Node.js versions - setup memo
read article
Elevate Your Gaming Setup: Affordable Alternatives for Setup Gaming
read article
How to set up HTMX in Django
read article
Créez Votre Configuration p Jeu Parfaite : Découvrez nos Offres dès Aujourd'hui !
read article
Kitty + Neovim: Aesthetic Dev Setup
read article
Um pouco de mim
read article
Pop_OS Japanese layout
read article
The fastest setup of enhanced conversions for Google Ads.
read article
zsh: command not found: brew
read article
Dill Processing Plant Plant Project Report 2024, Setup Details, Machinery Requirements and Cost Analysis
read article
Hair Oil Manufacturing Plant Project Report 2024, Cost, Industry Trends and Business Opportunities
read article
Glutamic Acid Manufacturing Plant Project Report 2024, Machinery, Cost and Raw Material Requirements
read article
How to set up Python for backend development
read article
Setting Up Your macOS Development Environment and Automating Backups.
read article
What is use for work as a developer - Basic Flutter setup
read article
Trabalhando com 2 pc's no setup,Dicas!🖥️
read article
The JavaScript Developer's Guide to Ubuntu on Windows 11 with WSL
read article
My Kubernetes Lab Setup - Using Vagrant & Docker
read article
Setup MacMini M1 for Development
read article
Setup MacMini M1 for Development
read article
How to install and setup Neovim with awesome plugins
read article
Redis Local setup
read article
Featured ones: