Skip to content

🟣 TypeScript Interview Questions Answered to help you get ready for your next full-stack developer interview.

Notifications You must be signed in to change notification settings

PrashantIndurkar/typescript-interview-questions

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

πŸ–² 39 TypeScript interview questions for web developers in 2023

TypeScript is a must know if you're a front developer or even a fullstack developer. Our curated list of TypeScript questions can be used for all levels of developers. Go through all questions and nail your next web developer tech interview in 2021.



You can also find all 39 answers here πŸ‘‰πŸΌ https://devinterview.io/dev/typescript-interview-questions


πŸ”Ή 1. What is Typescript and why one should use it?

Answer:

TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language.

Source: Stackoverflow.com   


πŸ”Ή 2. What are the benefits of TypeScript?

Answer:

TypeScript has following benefits.

  • It helps in code structuring.
  • Use class based object oriented programming.
  • Impose coding guidelines.
  • Offers type checking.
  • Compile time error checking.
  • Intellisense.
Source: talkingdotnet.com   


πŸ”Ή 3. What is TypeScript and why would I use it in place of JavaScript?

Answer:

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.
Source: stackoverflow.com   


πŸ”Ή 4. Explain generics in TypeScript

Answer:

Generics are able to create a component or function to work over a variety of types rather than a single one.

/** A class definition with a generic parameter */
class Queue<T> {
  private data = [];
  push = (item: T) => this.data.push(item);
  pop = (): T => this.data.shift();
}

const queue = new Queue<number>(); queue.push(0); queue.push("1"); // ERROR : cannot push a string. Only numbers allowed

Source: basarat.gitbooks.io   


πŸ”Ή 5. What are Modules in Typescript?

Answer:

Modules in Typescript helps in organizing the code. There are 2 types of Modulesβ€Šβ€”β€ŠInternal and External

  • Internal Modules are now replaceable by using Typescript’s namespace.

  • External Modules used to specify and load dependencies between multiple external js files. If there is only one js file used, then external modules are not relevant.

Source: Stackoverflow.com   


πŸ”Ή 6. List the built-in types in Typescript

Answer:

These are also called the primitive types in TypeScript:

  • Number type: it is used to represent number type values and represents double precision floating point values.
var variable_name: number;
  • String type: it represents a sequence of characters stored as Unicode UTF-16 code. It is the same as JavaScript primitive type.
var variable_name: string;
  • Boolean type: in Typescript, it is used to represent a logical value. When we use the Boolean type, we get output only in true or false. It is also the same as JavaScript primitive type.
var variable_name: bool;
  • Null type: it represents a null literal and it is not possible to directly reference the null type value itself.
var variable_name:number = null;
  • Undefined type: it is the type of undefined literal. This type of built-in type is the sub-type of all the types.
var variable_name:number = undefined;
Source: Stackoverflow.com   


πŸ”Ή 7. How to call base class constructor from child class in TypeScript?

Answer:

We can call base class constructor using super().



πŸ”Ή 8. Do we need to compile TypeScript files and why?

Answer:

Yes we do. Typescript is just a language Extension browsers can't interpret it. Converting from TypeScript to JavaScript is called compiling. Compiling doesn't mean binary code is created in this case. For this kind of translation, also the term transpilation is used instead of compilation.

Source: stackoverflow.com   


πŸ”Ή 9. What is TypeScript and why do we need it?

Answer:

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.

Source: talkingdotnet.com   


πŸ”Ή 10. What is "Decorators" in TypeScript?

Answer:

A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators are functions that take their target as the argument. With decorators we can run arbitrary code around the target execution or even entirely replace the target with a new definition.

There are 4 things we can decorate in ECMAScript2016 (and Typescript): constructors, methods, properties and parameters.

Source: www.sparkbit.pl   


πŸ”Ή 11. What is Interface in TypeScript?

Answer:

One of TypeScript’s core principles is that type-checking focuses on the shape that values have.

An interface is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes.

When you define your interface you’re saying that any object (not an instance of a class) given this contract must be an object containing interfaces properties.

Source: medium.com   


πŸ”Ή 12. What is the difference between Classes and Interfaces in Typescript?

Answer:

We use classes as object factories. A class defines a blueprint of what an object should look like and act like and then implements that blueprint by initialising class properties and defining methods. Classes are present throughout all the phases of our code.

Unlike classes, an interface is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes. Once code is transpiled to its target language, it will be stripped from interfaces.

A class may define a factory or a singleton by providing initialisation to its properties and implementation to its methods, an interface is simply a structural contract that defines what the properties of an object should have as a name and as a type.

Source: toddmotto.com   


πŸ”Ή 13. How to implement class constants in TypeScript?

Answer:

In TypeScript, the const keyword cannot be used to declare class properties. Doing so causes the compiler to an error with "A class member cannot have the 'const' keyword." TypeScript 2.0 has the readonly modifier:

class MyClass {
readonly myReadonlyProperty = 1;

<span class="token cMod">myMethod</span><span class="token cBase">(</span><span class="token cBase">)</span> <span class="token cBase">{</span>
    console<span class="token cBase">.</span><span class="token cMod">log</span><span class="token cBase">(</span><span class="token cVar">this</span><span class="token cBase">.</span>myReadonlyProperty<span class="token cBase">)</span><span class="token cBase">;</span>
<span class="token cBase">}</span>

}

new MyClass().myReadonlyProperty = 5; // error, readonly

Source: stackoverflow.com   


πŸ”Ή 14. What is getters/setters in TypeScript?

Answer:

TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object.

class foo {
private _bar:boolean = false;

get bar():boolean { return this._bar; } set bar(theBar:boolean) { this._bar = theBar; } }

var myBar = myFoo.bar; // correct (get) myFoo.bar = true; // correct (set)

Source: typescriptlang.org   


πŸ”Ή 15. Does TypeScript support all object oriented principles?

Answer:

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.

Source: jonathanmh.com   


πŸ”Ή 16. When to use interfaces and when to use classes in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 17. How could you check null and undefined in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 18. Which object oriented terms are supported by TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 19. What are the difference beetween Typescript and JavaScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 20. What is a TypeScript Map file?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 21. Could we use TypeScript on backend and how?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 22. What is Typings in Typescript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 23. What is the default access modifier for members of a class in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 24. Is that TypeScript code valid? Explain why.

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 25. How TypeScript is optionally statically typed language?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 26. How To Use external plain JavaScript Libraries in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 27. Does TypeScript supports function overloading?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 28. Explain how and why we could use property decorators in TS?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 29. What are different components of TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 30. How can you allow classes defined in a module to accessible outside of the module?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 31. What's wrong with that code?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 32. Are strongly-typed functions as parameters possible in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 33. What is the difference between "interface vs type" statements?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 34. What is one thing you would change about TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 35. Explain when to use "declare" keyword in TypeScript

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 36. Is it possible to generate TypeScript declaration files from JS library?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 37. What are Ambients in TypeScripts and when to use them?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 38. Explain why that code is marked as WRONG?

πŸ‘‰πŸΌ Check all 39 answers


πŸ”Ή 39. How would you overload a class constructor in TypeScript?

πŸ‘‰πŸΌ Check all 39 answers


About

🟣 TypeScript Interview Questions Answered to help you get ready for your next full-stack developer interview.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published