First "Hello World" Program in TypeScript

  • Certainly! Here's an example of a "Hello, World!" program written in TypeScript:


    // hello-world.ts

    // Declare a function that takes a string parameter and returns void
    function greet(name: string): void {
        console.log(`Hello, ${name}!`);
    }

    // Call the greet function with the name "World"
    greet("World");

  • To run this TypeScript program, you'll need to compile it to JavaScript first. Here's how you can do that:
  • Save the above code in a file called `hello-world.ts`.
  • Open a Command Prompt or PowerShell window.
  • Navigate to the directory where you saved `hello-world.ts`.
  • Run the TypeScript compiler (`tsc`) command to compile the TypeScript file into JavaScript:


    tsc hello-world.ts

  • This will generate a JavaScript file called `hello-world.js` in the same directory.
  • Finally, you can run the JavaScript file using Node.js:


    node hello-world.js

  • You should see the output:

Hello, World!

  • Congratulations! You have successfully written and executed a "Hello, World!" program in TypeScript.

No comments:

Post a Comment