Ans. JavaScript is the only client side language universally supported by all browsers. But JavaScript is not the best designed language. It’s not a class-based object-oriented language, doesn’t support class based inheritance, unreliable dynamic typing and lacks in compile time error checking. And TypeScript addresses all these problems. In other words, TypeScript is an attempt to “fix” JavaScript problems.
TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages. At the end of day “TypeScript is a language that generates plain JavaScript files.”
As stated on Typescript official website, “TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.” Where “typed” means that it considers the types of variables, parameters and functions.
Ans. TypeScript has following benefits:
Ans. There are mainly 3 components of TypeScript:
Ans. TypeScript can be installed and managed via npm, the Node.js package manager. To install TypeScript, first ensure the npm is installed properly. And then run the following command to install TypeScript globally on your system.
Hide Copy Code
npm install -g typescript
TypeScript is included in Visual Studio 2013 Update 2 and Visual Studio 2015 by default. TypeScript also provides support for other editors like Visual Studio Code, sublime, Emacs and Vim.
Ans. The extension for any TypeScript file is “.ts”. And any JavaScript file is TypeScript file as it is a superset of JavaScript. So change extension of “.js” to “.ts” file and your TypeScript file is ready. To compile any .ts file into .js, use the following command.
Hide Copy Code
tsc <TypeScript File Name>
For example, to compile “Helloworld.ts”:
Hide Copy Code
tsc helloworld.ts
And the result would be helloworld.js.
Ans. Yes, it's possible. While compiling add --outFILE [OutputJSFileName] option.
Hide Copy Code
tsc --outFile comman.js file1.ts file2.ts file3.ts
This will compile all 3 “.ts” file and output into single “comman.js” file. And what will happen if you don’t provide a output file name.
Hide Copy Code
tsc --outFile file1.ts file2.ts file3.ts
In this case, file2.ts and file3.ts will be compiled and the output will be placed in file1.ts. So now your file1.ts contains JavaScript code.
Ans. Yes. Using --watch compiler option, this can be achieved.
Hide Copy Code
tsc --watch file1.ts
This will first compile file1.ts in file.js and watch for the file changes. As soon as there is any change detected, it will compile it again. But you need to ensure that command prompt must not be closed, used with --watchoption.
Ans. The answer is YES. There are 4 main principles to Object Oriented Programming: Encapsulation, Inheritance, Abstraction, and Polymorphism. TypeScript can implement all four of them with its smaller and cleaner syntax. Read Write Object-Oriented JavaScript with TypeScript.
Ans. TypeScript supports the following object oriented terms:
Ans. TypeScript supports the following data types:
Hide Copy Code
enum Color {Red, Green, Blue};
var c: Color = Color.Green;
Hide Copy Code
function NoReturnType(): void {}
Ans. TypeScript is referred as optionally statically typed, which means you can ask the compiler to ignore the type of a variable. Using any data type, we can assign any type of value to the variable. TypeScript will not give any error checking during compilation.
Hide Copy Code
var unknownType: any = 4;
unknownType = "Okay, I am a string";
unknownType = false; // A boolean.
Ans. Modules are the way to organize code in TypeScript. Modules don’t have any features, but can contain classes and interfaces. It is same like namespace in C#.
Ans. The concept of classes is very similar to .NET/Java. A Class can have constructor, member variables, properties and methods. TypeScript also allows access modifiers “private” and “public” for member variables and functions.
Ans. Using extends keyword, we can implement inheritance.
Hide Copy Code
class Animal {
public domestic:boolean;
constructor(public name: string) { }
}
class Cat extends Animal {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = true;
}
}
class Tiger extends Animal {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = false;
}
}
Ans. Using super(), we can call base class constructor, as seen in the above code.
Ans. In TypeScript, each member of class is public by default.
Ans. Classed define in a module are available within the module. Outside the module, you can’t access them.
Hide Copy Code
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.
Hide Copy Code
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");
Ans. In JavaScript, every parameter is considered optional. If no value is supplied, then it is treated as undefined. So while writing functions in TypeScript, we can make a parameter optional using the “?” after the parameter name.
Hide Copy Code
function Demo(arg1: number, arg2? :number) {
}
So, arg1 is always required and arg2 is an optional parameter. Remember, optional parameters must follow required parameters. If we want to make arg1 optional, instead of arg2, then we need to change the order and arg1 must be put after arg2.
Hide Copy Code
function Demo(arg2: number, arg1? :number) {
}
Similar to optional parameters, default parameters are also supported.
Hide Copy Code
function Demo(arg1: number, arg2 = 4) {
}
Ans. Yes, TypeScript does support function overloading. But the implementation is odd. When you overload in TypeScript, you only have one implementation with multiple signatures.
Hide Copy Code
class Customer {
name: string;
Id: number;
add(Id: number);
add(name:string);
add(value: any) {
if (value && typeof value == "number") {
//Do something
}
if (value && typeof value == "string") {
//Do Something
}
}
}
The first signature has one parameter of type number whereas the second signature has a parameter of type string. The third function contains the actual implementation and has a parameter of type any. The any data type can take any type of data. The implementation then checks for the type of the supplied parameter and executes a different piece of code based on supplier parameter type.
OR You can also use union type introduced in TypeScript 1.4. Union types let you represent a value which is one of multiple types.
Hide Copy Code
add(a:string|number) {
//do something
}
Using union type, you can typically remove the need for an overload.
Ans. Yes, it is possible. To debug it, you need .js source map file. If you are new to source map, read more here. So compile the .ts file with the --sourcemap flag to generate a source map file.
Hide Copy Code
tsc -sourcemap file1.ts
This will create file1.js and file1.js.map. And last line of file1.js would be reference of source map file.
Hide Copy Code
//# sourceMappingURL=file1.js.map
Ans. TypeScript Definition Manager (TSD) is a package manager to search and install TypeScript definition files directly from the community driven DefinitelyTyped repository. Let’s see with an example.
Suppose, you want to use some jQuery code in your .ts file.
Hide Copy Code
$(document).ready(function() { //Your jQuery code });
And now when you try to compile it using tsc, you will get compile time error Cannot find name “$”. That’s because TypeScript can’t understand what does “$” means. So somehow we need to inform TypeScript compiler that it belongs to jQuery. That’s where TSD comes into play. You can download jQuery Type Definition file and include it in your .ts file. First, install TSD.
Hide Copy Code
npm install tsd -g
In your typescript directory, create a new typescript project by running:
Hide Copy Code
tsd init
Then install the definition file for jquery.
Hide Copy Code
tsd query jquery --action install
This will download and create a new directory containing your jquery definition file. The definition file ends with “.d.ts”. So now include it by updating your typescript file to point to the jquery definition.
Hide Copy Code
/// <reference path="typings/jquery/jquery.d.ts" />
$(document).ready(function() { //To Do
});
Now try to compile again and this time, js will be generated without any error.
Ans. It’s quite possible that JavaScript libraries/frameworks don’t have TypeScript definition files and yet you want to use them without any errors. Te solution is to use the declare keyword. The declare keyword is used for ambient declarations where you want to define a variable that may not have originated from a TypeScript file.
Hide Copy Code
declare var unKnownLibrary;
TypeScript runtime will assign unKnownLibrary variable any type. You won’t get Intellisense in design time but you will be able to use the library in your code.
Ans. You can generate TypeScript definition file from any .ts file via tsc compiler. Generating a TypeScript definition will make your TypeScript file reusable.
Hide Copy Code
tsc --declaration file1.ts
Ans. The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project. And using this file, we can streamline building TypeScript project. Below is a sample tsconfig.json file.
Hide Copy Code
{
"compilerOptions": {
"removeComments": true,
"sourceMap": true
},
"files": [
"main.ts",
"othermodule.ts"
]
}
Within files section, define all the .ts files in the project. And when you invoke tsc without any other arguments with the above file in the current directory, it will compile all the files with the given compiler option settings.
Ans. Well, TypeScript is great but there are some disadvantages as well.