What is TypeScript?

TypeScript is an open-source programming language which is a superset of JavaScript. It is an object-oriented and tightly typed programming language maintained by Microsoft. TypeScript code is transformed to JavaScript, which used in environment that supports JavaScript, including browsers, Node.js, and your own applications.

TypeScript is a variant of JavaScript with a few more features like highly scalable, namespaces, generics, decorators, abstract classes, static typing, conditional type etc.

Explain how the arrays work in TypeScript.

Array is a collection of values of the same data type. Arrays are ordered and indexed collections of values where the indexing starts from 0, i.e., the first element has index 0, the second has index 1, and so on.

Syntax

// Declaration
let myArray: number[] = [];

assigning values
myArray[0] = 10;
myArray[1] = 20;
myArray[1] = 30;

Short-hand Syntax

let myArray: number[] = [10, 20, 30];

Alternate Syntax

let myArray: Array<number> = [10, 20, 30];

What are the benefits of using TypeScript?

Some benefits of TypeScript are:

  • Cross-Platform: Usable across platforms, in both client and server-side projects due to versatile transpiling.
  • Easy Debugging: Easy debugging due to advanced debugger that focuses on catching logical errors before compile-time.
  • Less Syntactical Clutter: TypeScript is more expressive, meaning it has less syntactical clutter.
  • Scalability and Maintainability: The ability to describe the shape of objects and functions directly in your code makes your codebase easier to understand and more predictable. 
  • Static Typing: Static typing makes TypeScript easier to read and more structured than JavaScript’s dynamic typing.
  • Tooling: TypeScript is a strongly typed language that uses type inferences. These characteristics open the doors to better tooling and tighter integration with code editors. 

What are the primitive types in TypeScript?

TypeScript has three primitive types that are frequently used:

  1. boolean: value that can have either a ‘true’ or ‘false’ value.

    let myBool: bool = true;
  2. number:  numeric values like 1, 10, 20.5 etc. All the numbers in TypeScript are stored in floating point value:

    let myNum: number = 10;
  3. string: It represents sequence of characters such as "hello", "typescript", etc. It is stored as UTF-16 code and enclosed within single or double quotes.

    let myString: string = "Hello TypeScript";

Other built-in types are:

  • any: Represents any type of value and allows a variable to store multiple types.
  • null: Represents the absence of a value intentionally assigned to a variable.

    let myNum: number = null;
  • undefined: Represents a variable that has not yet been assigned a value.

    let myNum: number = undefined;
  • void: Represents the lack of a type, commonly used as the return type for functions that do not return a value.

     let unusable: void = undefined;

What are the main features of TypeScript?

Here are the main features of TypeScripts:

  • Cross-Platform: TypeScript compiler can be installed on any operating system including Windows, MacOS and Linux
  • DOM Manipulation: You can use TypeScript to manipulate the DOM for adding or removing client-side web page elements.
  • ES6 Features: TypeScript includes most features of planned ECMAScript 2015 (ES6) such as Arrow functions.
  • Object-Oriented Language: TypeScript provides all the standard OOP features like classes, interfaces, and modules.
  • Optional Static Typing: TypeScript also allows optional static typing in case you are used to the dynamic typing of JavaScript.
  • Static Type-Checking: TypeScript uses static typing and helps type checking at compile time. Thus, you can find compile-time errors while writing the code without running the script.
teststep banner