tags: #TypeScript #GettingStarted
TypeScript is a strongly typed superset of JavaScript that compiles to plain JavaScript. It enhances JavaScript by adding static types, making the code more maintainable and scalable.
- Static Typing: Helps catch errors at compile time.
- Better Code Completion: IDEs provide enhanced autocompletion and suggestions.
- Improved Maintainability: Easier to refactor and understand large codebases.
- Modern JavaScript Features: Supports ES6+ syntax and compiles down to compatible JavaScript.
// TypeScript example
let message: string = "Hello, TypeScript!";
console.log(message);To install TypeScript globally, use npm:
npm install -g typescriptVerify the installation:
tsc --versionYou can also install TypeScript locally within a project:
npm install --save-dev typescriptTo compile a TypeScript file (app.ts) into JavaScript:
tsc app.tsThis generates app.js, which can be executed with Node.js:
node app.jsVS Code is one of the best editors for TypeScript.
- Download & install VS Code.
- Install the TypeScript Language Support extension (optional).
- Open your project folder and create a TypeScript file (
index.ts). - Compile and run the file using
tsc.
To configure TypeScript settings, create a tsconfig.json file:
tsc --initThis generates a default tsconfig.json, where you can specify compiler options:
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"strict": true,
"outDir": "./dist"
}
}Now, compile the entire project:
tscWith these steps, you’re ready to start coding in TypeScript! 🚀