Follow me from time to time to share your highlights

Source: Web front-end development

TypeScript is an open source superset of JavaScript developed by Microsoft to add additional functionality without breaking existing programs.

Because of its unique advantages, such as static typing and many shorthand notations, TypeScript is now widely used by front-end and full-stack developers on large projects. Today, we’re going to look at 30 TypeScript interview questions and answers to help you prepare for a TypeScript interview.

1. What are TypeScript’s main features?

  • Cross-platform: The TypeScript compiler can be installed on any operating system, including Windows, macOS, and Linux.
  • ES6 features: TypeScript contains most of the planned ECMAScript 2015 (ES6) features, such as arrow functions.
  • Object-oriented languages: TypeScript provides all the standard OOP functionality, such as classes, interfaces, and modules.
  • Static type checking: TypeScript uses static typing and AIDS type checking at compile time. As a result, you can find compile-time errors while writing code without having to run the script.
  • Optional static typing: TypeScript also allows optional static typing if you’re used to JavaScript’s dynamic typing.
  • DOM manipulation: You can use TypeScript to manipulate the DOM to add or remove client-side web elements.

2. What are the benefits of using TypeScript?

  • TypeScript is more expressive, which means it has less syntactic clutter.
  • Because the advanced debugger focuses on catching logic errors before compile time, debugging is easy.
  • Static typing makes TypeScript easier to read and structure than JavaScript’s dynamic typing.
  • It can be used across platforms, in both client-side and server-side projects, due to generic translation.

What are TypeScript’s built-in data types?

Numeric types: Values used to represent numeric types. 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 indicates a variable whose value is not defined.

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: The type assigned to a method that does not return a value.

let unusable: void = undefined;
Copy the code

4. What is the current stable version of TypeScript?

The current stable release is 4.2.3.

What are TypeScript interfaces?

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

An interface is an interface defined with a keyword that can contain property and method declarations that use a function or arrow function.

interface IEmployee {
Copy the code

6. What are modules in TypeScript?

Modules in TypeScript are collections of related variables, functions, classes, and interfaces.

You can think of a module as a container that contains everything you need to perform a task. Modules can be imported to easily share code between projects.

module module_name{
Copy the code

7. How to use TypeScript on the back end?

You can use Node.js with TypeScript to bring TypeScript’s benefits to the back end.

To install the TypeScript compiler into your node.js, simply type the following command:

npm i -g typescript
Copy the code

What are type assertions in TypeScript?

Type assertions in TypeScript work like type conversions in other languages, but without the type checking or data reorganization that is possible in languages like C# and Java. Type assertions have no impact on the runtime and are used only by the compiler.

Type assertions are essentially soft versions of type conversions, suggesting that the compiler treat a variable as one type, but not force it into the model if it is in a different form.

How to 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

Lets are the default way to declare variables in TypeScript. Compared to var, lets reduce the number of compile-time errors and improve the readability of your code.

let num:number = 1;
Copy the code

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

const num:number = 100;
Copy the code

How do we call base class constructors from subclasses in TypeScript?

You can use the super() function to call the constructor of the base class.

class Animal {
Copy the code

11. Explain how to use TypeScript mixin.

Mixins are essentially inheritance that works in the opposite direction. Mixins allow you to build new classes by combining some of the simpler class Settings of previous classes.

Instead, class A inherits its functionality from class B, and class B needs to return the additional functionality of A new class from class A.

How to check null and undefined in TypeScript?

You can use juggle-check, which checks for null and undefined, or urt-check, which returns true set to null and does not evaluate variables undefined by True.

//juggle
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 according to 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 not see its current value. These are essential to implementing encapsulation.

For example, a new employer might be able to find out the number of employees at get, but not set.

const fullNameMaxLength = 10;
Copy the code

How do I allow classes defined outside a module to be accessible?

You can use the export keyword to open a module for use outside the module.

module Admin {
Copy the code

How to convert strings to numbers using Typescript?

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

var x = "32";
Copy the code

16. What is a.map file and why/how to use it?

The a.map file is the source map showing how the original typescript code was interpreted into usable JavaScript code. They help simplify debugging because you can catch any weird compiler behavior.

Debugging tools can also use these files to allow you to edit the underlying TypeScript instead of the emitted JavaScript files.

What are classes in TypeScript? How do you define them?

Class represents the shared behavior and properties of a set of related objects.

For example, our class might be Student, and all of its objects have this attendClass method. John on the other hand is a separate type instance, Student probably has additional unique activities like attendextracurfinancial-related.

You declare the class class with the keyword:

class Student {    
Copy the code

18. How does TypeScript relate to JavaScript?

TypeScript is an open source syntax superset for JavaScript and can be compiled to JavaScript. All the original JavaScript libraries and syntax are still valid, but TypeScript adds additional syntax options and compiler features not found in JavaScript.

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

What is JSX in TypeScript?

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

What JSX modes 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 to JSX. Instead, react. CreateElement is emitted and output as a.js file extension.

  • React-native is combined with Preserve, React because it maintains all JSX and outputs 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
Copy the code

22. What scope are available in TypeScript? How does this compare with 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/block: Variables defined in a local scope can be used anywhere in the block.

What are arrow /lambda functions in TypeScript?

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

24. Interpret REST parameters and rules for declaring REST parameters.

The remaining arguments allow you to pass a different number of arguments (zero or more) to the function. This is useful when you are not sure how many arguments the function will take. All parameters after the remaining symbols… Will be stored in an array. Such as:

function Greet(greeting: string, ... names: string[]) {Copy the code

The REST parameter must be the last one defined by the parameter, and each function can have only one REST parameter.

What is the triple slash command? What are the three slash commands?

Triple-slash instructions are single-line comments that contain XML tags that are used as compiler directives. Each instruction represents what to load during compilation. The triple slash directive only works at the top of its file and will be treated as a normal comment anywhere else in the file.

  • /// is the most common directive that defines dependencies between files.

  • /// Is similar to path but defines package dependencies.

  • /// allows you to explicitly include built-in lib files.

26, what’s the effect of Omit type?

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

Omit<Type, Keys>
Copy the code

Such as:

interface Todo {
Copy the code

How to implement function overloading in TypeScript?

To overload a function in TypeScript, you simply create two functions with the same name but different argument/return types. Both functions must take the same number of arguments. This is an important part of polymorphism in TypeScript.

For example, you can create an add function that adds the two arguments if they are numbers, or concatenates them if they are strings.

function add(a:string, b:string):string;
Copy the code

28. How to make all attributes of an interface optional?

You can easily make all properties optional using the Partial mapping type.

29. When should I use the keyword unknown?

Unknown, if you do not know what type to expect up front but want to allocate it later, the any keyword should be used and the keyword will not work.

30. What are decorators and what can they be applied to?

Decorators are special declarations that allow you to modify a class or class member once by using the @ annotation tag. Each decorator must refer to a function that will be evaluated at run time.

For example, the decorator @sealed will correspond to the sealed function. Any labeled @sealed will be used to evaluate the sealed function.

function sealed(target) {
Copy the code

They can be attached to:

  • Class declaration

  • methods

  • accessories

  • features

  • parameter

Note: Decorators are not enabled by default. To enable them, you must edit the fields in the compiler options in experimentalDecorators from the tsconfig.json file or the command line. If there is infringement, please contact xiaobian to delete