To read this with better formatting, right-click on the file in Explorer, choose "Open With", then select "Markdown Preview".
This is a small project aimed at demonstrating how to start coding with TypeScript without worrying about compiling it each time you need to run your code. TypeScript is awesome, but the fact that we need to compile it to JavaScript can be annoying, especially for beginners. So, here are a few tips to help you start coding!
- Required
- VS Code and Extensions
- Prepare Your Local Repo
- Install TypeScript on Your Repo
- Update tsconfig.json File
- NPM Configuration
- Create Your First TypeScript Code
- Configure Auto Compile and Run Current File Code
- Run Your Code
- Update Your Code and Run It Again
- Optionals
VS Code is mandatory but extensions are optional but highly recommended
- Create a new folder
- Open New Terminal on VS Code
- Command:
git init
- Create
.gitignore
file - Create
README.md
file - Open Source Control on VS Code and commit your changes
- Publish your branch
- Create a branch from master (Optional)
- Command 1:
npm install -g typescript
- Command 2:
tsc --init
(tsconfig.json will be added automatically to your project folder, no need to modify for now)
- Change line to
"outDir": "./output",
- Uncomment
sourceMap
line
- Command:
npm init
- Open
package.json
file that was created on the step before and add the following line inside "scripts":"build": "tsc"
- Add a
src
folder - Add an
output
folder - Add
<nameYouWant>.ts
file intosrc
folder - Add the following code to it:
class MyFirstCode { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } class Greetings extends MyFirstCode { greet() { console.log(`Hello ${this.name}! Congrats! Your age is ${this.age}!`); } } const person = new Greetings("John", 25); person.greet();
- Open
RUN AND DEBUG
menu on VS Code - Select
create a launch.json file
- Select
Node.js
option - Add the following lines inside configurations:
"preLaunchTask": "npm: build", "console": "integratedTerminal",
- Modify line to
"program": "${file}",
- Press
F5
and see the magic happens!🧙♂️- JavaScript files should be created into
output
folder - Terminal should open with compiling status
- After compiling, TypeScript code should run successfully
- JavaScript files should be created into
- It should update and run automatically according to your changes 🤖
- Add
output
folder to .gitignore - Commit your changes to your repository
- Check out your GitHub repository
- Always commit your changes and follow it with GitLens
- Add ESLint integration to your project (Google it)
- Add Husky integration and pre-commit configurations (Google it)
❤️💻HAPPY CODING!!!💻❤️