1. What are TypeScript’s key features?

  • Cross-platform: the TypeScript compiler can be installed on any operating system, including Windows, macOS, and Linux.

  • ES6 features: TypeScript includes most of the planned ECMAScript 2015 (ES6) features, such as arrow functions.

  • Object-oriented language: TypeScript provides all standard OOP functionality, such as classes, interfaces, and modules.

  • Static type checking: TypeScript uses static typing and helps with type checking at compile time. As a result, you can find compile-time errors while writing code without having to run scripts.

  • Optional static typing: If you’re used to dynamic typing in JavaScript, TypeScript also allows optional static typing.

  • DOM manipulation: You can manipulate the DOM using TypeScript to add or remove client-side web page elements.

2. What are the benefits of using TypeScript?

  • TypeScript is more expressive, which means its syntax is less cluttered.

  • Because advanced debuggers focus on catching logic errors before compile time, debugging is easy.

  • Static typing makes TypeScript easier to read and more structured than JavaScript’s dynamic typing.

  • Due to generic translation, it can be used cross-platform, both in client and server projects

What are TypeScript’s built-in data types?

Numeric type: A value used to represent a numeric type. All numbers in TypeScript are stored as floating point values.

let identifier: number = value;
Copy the code

Boolean type: A logical binary switch containing true or false

let identifier: string = " ";
Copy the code

Null type: Null denotes a variable whose value is undefined.

let identifier: bool = Boolean value;
Copy the code

Undefined type: An undefined literal that is the starting point for all variables.

let num: number = null;
Copy the code

Void type: Type assigned to a method that does not return a value.

let unusable: void = undefined;
Copy the code

4. What are interfaces in TypeScript?

An interface defines a contract or structure for an object that uses the interface.

An interface is a keyword-defined interface that can contain property and method declarations using functions or arrow functions.

interface IEmployee {    
                       empCode: number;    
                       empName: string;    
                        getSalary: (number) => number; // arrow function    
                        getManagerName(number): string; 
                   }
Copy the code

How do I create variables in TypeScript?

You can create variables in three ways: var, let, and const. Var is the old style of strictly scoped variables. You should avoid using VAR whenever possible because it can cause problems in larger projects.

var num:number = 1;
Copy the code

Let is the default way to declare variables in TypeScript. Compared to VAR, let reduces the number of compile-time errors and improves the readability of code.

let num:number = 1;
Copy the code

Const creates a constant variable whose value cannot be changed. It uses the same scope rules, lets, and helps reduce the overall complexity of the program.

const num:number = 100;
Copy the code

How do you check null and undefined in TypeScript?

You can use juggle-check, which checks for null and undefined, or strict-check, which returns values where true is set to null and does not evaluate variables that are not defined by true.

//juggleif (x == null) {  }
var a: number;  
var b: number = null;  function check(x, name) { 
     if (x == null) {  
      console.log(name + ' == null');  
    }  
     if (x === null) {  
     console.log(name + ' === null');  
     }
     if (typeof x === 'undefined') { 
      console.log(name + ' is undefined');  
      }
   }   
check(a, 'a');
check(b, 'b');
Copy the code

What are getters/setters in TypeScript? How do you use them?

Getters and setters are special types of methods that help you delegate different levels of access to private variables depending on the needs of your program.

Getters allows you to reference a value but not edit it. Setters allow you to change the value of a variable, but do not view its current value. These are essential for implementing encapsulation.

How do I convert strings to numbers using Typescript?

Like JavaScript, you can use the parseInt or parseFloat functions to convert strings to integers or floats, respectively. You can also use the unary operator + to convert the string to the most appropriate number type, with “3” becoming an integer, 3 and “3.14” becoming floating point 3.14.

var x = "32";
var y: number = +x;
Copy the code

9. What’s TypeScript’s relationship to JavaScript?

TypeScript is an open source syntactic superset of JavaScript that compiles to JavaScript. All of the original JavaScript libraries and syntax are still valid, but TypeScript adds additional syntax options and compiler capabilities not found in JavaScript.

TypeScript also works with most of the same technical interfaces as JavaScript, such as Angular and jQuery.

10. What is JSX in TypeScript?

JSX is an embeddable XML-like syntax that allows you to create HTML. TypeScript supports embedding, type checking, and compiling JSX directly into JavaScript.

What JSX schemas does TypeScript support?

TypeScript has built-in support for Preserve, React, and React-Native.

  • Preserve keeps JSX intact for subsequent conversions.

  • React is not converted by JSX. Instead, react. CreateElement is emitted and output as a.js file extension.

  • React native combines preserve, React because it maintains all JSX and output as.js extensions

How do I compile TypeScript files

You need to call the TypeScript compiler TSC to compile the file. You need to install the TypeScript compiler. You can use NPM.

npm install -g typescript
tsc <TypeScript File Name>
Copy the code

What scopes are available in TypeScript? How does this compare to JS

  • Global scope: Defined outside of any class and can be used anywhere in the program.

  • Function/class scope: Variables defined in a function or class can be used anywhere within that scope.

  • Local scope/code block: Variables defined in a local scope can be used anywhere in the block.

14. What are arrow /lambda functions in TypeScript?

The fat arrow function is a shorthand syntax for defining function expressions for anonymous functions. It is similar to lambda functions in other languages. The arrow function lets you skip the function keyword and write cleaner code.

15. What’s the use of Omit types?

Omit is a form of the utility type that facilitates common type conversions. Omit Omit allows you to construct types by passing current types and selecting Keys to Omit in the new Type.

Omit<Type, Keys>
Copy the code

Such as:

interface Todo {  
   title: string;  
   description: string;  
   completed: boolean;  
   createdAt: number;
}
type TodoPreview = Omit<Todo, "description">;
Copy the code