Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get started with a simple TypeScript application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 281
    Comment on it

    Typescript is Microsoft’s extension of Javascript. It provides type checking abilities and also has features of object oriented programming. TypeScript is used in both Angular2 and Ionic2.
    TypeScript has similar syntax and semantics that are used in javascript by developers.

    Installing TypeScript
    You can install TypeScript tools in two ways:

    • By npm (the Node.js package manager)
    • By installing Visual Studio plugins of TypeScript

    Run the following command to install TypeScript:
     

    npm install -g typescript

    Build TypeScript File:
    Write the following code in your text editor and save it as hello.ts :

    function hello(name) {
        return "Hello, " + name;
    }
    
    var user = user name;
    
    document.body.innerHTML = hello(user);

    Compile the code:
    Run this TypeScript compiler tsc greeter.ts

    tsc hello.ts

    It will create a greeter.js file with the same javascript code that is written in ts file.

    Type annotations:
    There are some ways in TypeScript that can be used to record the intended contract of the variable or function. Here, we are passing string as a parameter in hello function:

    function hello(name: string) {
        return "Hello, " + name;
    }
    
    var user = user name;
    
    document.body.innerHTML = hello(user);

    here we have added :string type annotation to hello function as argument.

    Running your TypeScript web app:
    Now create a html file hello.html with the below code and run it in browser to see the result.

    <!DOCTYPE html>
    <html>
        <head><title>TypeScript hello</title></head>
        <body>
            <script src=hello.js"></script>
        </body>
    </html>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: