Hello, World! in TypeScript
1. Welcome and Icebreaker:
- Great to have you! What’s your favorite programming language and why?
2. Outline the Session:
- In this session, we’ll write our first TypeScript program: “Hello, World!”.
- We will learn how to compile TypeScript code into JavaScript and then run it.
- Learning Outcomes: Understand the basic TypeScript workflow (write -> compile -> run).
- We’ll keep this interactive, so please ask if anything is unclear.
3. Concept Explanation:
- TypeScript Files (.ts): We write TypeScript code in files with a
.tsextension. - Compilation: Browsers and Node.js don’t understand TypeScript directly. We need to compile our
.tsfiles into JavaScript (.js) files using the TypeScript compiler (tsc). - Running JavaScript: Once compiled, the resulting
.jsfile can be run by Node.js or in a web browser.
4. Live Coding Demonstration:
- Create a TypeScript file:
- Open VS Code and your project folder (e.g.,
my-typescript-projects). - Create a new file named
hello.ts. - Add the following code to
hello.ts:
let message: string = "Hello, World!"; console.log(message); - Open VS Code and your project folder (e.g.,
- Compile the TypeScript file:
- Open the integrated terminal in VS Code (if not already open).
- Navigate to the directory where you saved
hello.ts. - Run the TypeScript compiler:
tsc hello.ts- This will create a new file named
hello.jsin the same directory.
- Run the JavaScript file:
- In the terminal, run the
hello.jsfile using Node.js:
node hello.js- You should see “Hello, World!” printed to the console.
- In the terminal, run the
5. Tasks:
- Short Exercise:
- Create a new TypeScript file named
greeting.ts. - In
greeting.ts, declare a variable to store your name and print a personalized greeting (e.g., “Hello, [Your Name]!”). - Compile
greeting.tstogreeting.js. - Run
greeting.jsusing Node.js and verify the output.
- Create a new TypeScript file named