• Register
8,210 points
15 10 7
  1. What is TypeScript?

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It is pure object-oriented with classes, interfaces and statically typed programming languages like C# or Java. You will need a compiler to compile and generate the code in the JavaScript file. Basically, TypeScript is the ES6 version of JavaScript with some additional features.

Syntax:

1

2

var message:string = "Welcome to Edureka!"

console.log(message)

A TypeScript code is written in a file with .ts extension and then compiled into JavaScript using the compiler. You can write the file in any code editor and the compiler needs to be installed on your platform. After the installation, the command tsc <filename>.ts compiles the TypeScript code into a plain JavaScript file.

  1. Why should i use TypeScript in place of JavaScript and how is it different from JavaScript?

TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code. For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.

In details:

●       TypeScript supports new ECMAScript standards and compiles them to (older) ECMAScript targets of your choosing. This means that you can use features of ES2015 and beyond, like modules, lambda functions, classes, the spread operator, destructuring, today.

●       JavaScript code is valid TypeScript code; TypeScript is a superset of JavaScript.

●       TypeScript adds type support to JavaScript. The type system of TypeScript is relatively rich and includes: interfaces, enums, hybrid types, generics, union and intersection types, access modifiers and much more. TypeScript makes typing a bit easier and a lot less explicit by the usage of type inference.

●       The development experience with TypeScript is a great improvement over JavaScript. The IDE is informed in real-time by the TypeScript compiler on its rich type information.

●       With strict null checks enabled (--strictNullChecks compiler flag) the TypeScript compiler will not allow undefined to be assigned to a variable unless you explicitly declare it to be of nullable type.

●       To use TypeScript you need a build process to compile to JavaScript code. The TypeScript compiler can inline source map information in the generated .js files or create separate .map files. This makes it possible for you to set breakpoints and inspect variables during runtime directly on your TypeScript code.

●       TypeScript is open source (Apache 2 licensed, see github) and backed by Microsoft. Anders Hejlsberg, the lead architect of C# is spearheading the project.

 

Difference between TypeScript and JavaScript

 

TYPESCRIPT

JAVASCRIPT

TypeScript is an Object-Oriented language

JavaScript is a Scripting language

It has a feature known as Static typing

It does not have static typing

TypeScript gives support for modules

JavaScript does not support modules

It supports optional parameter function

It does not support optional parameter function

 

  1. What is Triple-Slash Directive? What are some of the triple-slash directives?

Triple-slash directives are single-line comments containing a single XML tag. The contents of the comment are used as compiler directives.

Triple-slash directives are only valid at the top of their containing file. A triple-slash directive can only be preceded by single or multi-line comments, including other triple-slash directives. If they are encountered following a statement or a declaration they are treated as regular single-line comments, and hold no special meaning. Below are some of the triple-slash directives in TypeScript:

●      The /// <reference path="..." /> directive is the most common of this group. It serves as a declaration of dependency between files. Triple-slash references instruct the compiler to include additional files in the compilation process. If the compiler flag --noResolve is specified, triple-slash references are ignored; they neither result in adding new files, nor change the order of the files provided.

●      Similar to a /// <reference path="..." /> directive, this directive serves as a declaration of dependency; a /// <reference types="..." /> directive, however, declares a dependency on a package. For example, including /// <reference types="node" /> in a declaration file declares that this file uses names declared in @types/node/index.d.ts; and thus, this package needs to be included in the compilation along with the declaration file.

  1. How can you allow classes defined in a module to accessible outside of the module?

Classes define in a module are available within the module. Outside the module you can’t access them.

 

module Vehicle {

    class Car {

        constructor (

            public make: string,

            public model: string) { }

    }

    var audiCar = new Car("Audi", "Q7");

}

// This won't work

var fordCar = Vehicle.Car("Ford", "Figo");

 

As per above code, fordCar variable will give us compile time error. To make classes accessible outside module use export keyword for classes.

 

module Vehicle {

    export class Car {

        constructor (

            public make: string,

            public model: string) { }

    }

    var audiCar = new Car("Audi", "Q7");

}

// This works now

var fordCar = Vehicle.Car("Ford", "Figo");

 

 

  1. Is it possible to combine multiple .ts files into a single .js file? If yes, then how?

Yes, it is possible. For this, we need to add --outFILE [OutputJSFileName] compiling option.

 

$ tsc --outFile comman.js file1.ts file2.ts file3.ts 

The above command will compile all three ".ts"file and result will be stored into single "comman.js" file. In the case, when we don't provide an output file name as like in below command.

 

$ tsc --outFile file1.ts file2.ts file3.ts 

Then, the file2.ts and file3.ts will be compiled, and the output will be placed in file1.ts. So now our file1.ts contains JavaScript code.

 

  1. Could we use TypeScript on the backend? If yes, how?

Yes, we can use TypeScript on the backend. We can understand it with the following example. Here, we choose Node.js and have some additional type safety and the other abstraction that the language brings.

 

Install Typescript compiler

npm i -g typescript 

The TypeScript compiler takes options in the tsconfig.json file. This file determines where to put built files.

  "compilerOptions": { 

    "target": "es5", 

 

    "module": "commonjs", 

    "declaration": true, 

    "outDir": "build" 

  } 

Compile ts files

tsc 

Run

node build/index.js 

 

  1. What are Generics and Modules in TypeScript?

Generics:

Generics in Typescript is no different than generics in other programming languages like C# or Java.

You can create a class, an interface, or a function that works with different types, without specifying the type upfront.

function greet(a : T) {

  console.log(`Hi ${a}!`)

}

greet('DS'); //function call

 

The symbol T identifies a generic type.

 

Modules:

A module is a way to construct a local scope in a file. So that all the classes, variables declared in a module are not accessible outside the module.

<!--[if gte vml 1]> <![endif]-->&#128073; We can create a module using the export keyword.

<!--[if gte vml 1]> <![endif]-->&#128073; A module in typescript can be used in another module using the import keyword.

export class Student {

    readonly Id: number;

    Name: string;

   

    constructor(id: number, name: string) {

        this.Id = id;

        this.Name = name;

    }

}

 

let Subject: string = "Computer Science";

 

 

 

8,210 points
15 10 7