|
| 1 | +// 📌 JavaScript Advanced - Introduction to TypeScript |
| 2 | +// Welcome to the TypeScript section of the JavaScript Advanced tutorial! |
| 3 | +// Here, you'll learn how TypeScript enhances JavaScript with static typing. |
| 4 | +// Installing TypeScript |
| 5 | +// Run the following command to install TypeScript globally: |
| 6 | +// npm install -g typescript |
| 7 | +// Declaring Variables with Types |
| 8 | +var message = "Hello, TypeScript!"; |
| 9 | +var count = 42; |
| 10 | +var isActive = true; |
| 11 | +// Functions with Type Annotations |
| 12 | +function greet(name) { |
| 13 | + return "Hello, ".concat(name, "!"); |
| 14 | +} |
| 15 | +console.log(greet("John")); |
| 16 | +var user = { id: 1, name: "Alice", isAdmin: false }; |
| 17 | +console.log(user); |
| 18 | +// Using Classes with TypeScript |
| 19 | +var Person = /** @class */ (function () { |
| 20 | + function Person(name) { |
| 21 | + this.name = name; |
| 22 | + } |
| 23 | + Person.prototype.sayHello = function () { |
| 24 | + console.log("Hello, my name is ".concat(this.name)); |
| 25 | + }; |
| 26 | + return Person; |
| 27 | +})(); |
| 28 | +var person = new Person("Emma"); |
| 29 | +person.sayHello(); |
| 30 | + |
| 31 | +// Explain how to run this file |
| 32 | +// To run this file, you can use the following command: |
| 33 | +// tsc advanced\ts_intro.ts |
| 34 | +// This will compile the TypeScript code into JavaScript. |
| 35 | +// Then, you can run the generated JavaScript file: |
| 36 | +// node advanced\ts_intro.js |
| 37 | +// will execute the JavaScript code and produce the output: |
| 38 | +// Hello, TypeScript!// Hello, John! // { id: 1, name: 'Alice', isAdmin: false } |
| 39 | +// Hello, my name is Emma |
| 40 | + |
| 41 | +// 💡 TypeScript helps prevent errors by enforcing type safety and better code structure! |
0 commit comments