Prev Next

Web / Typescript interview questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is typescript?

TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language.

Using types is completely optional. Typescript compiles/transpiles to regular JS. Typescript can be used for Front End as well as the backend with Node.JS. Types from 3rd party libraries can be added with type definitions.

2. Advantages of using typescript.
  • TypeScript is Object-oriented.
  • TypeScript transpiler generates compilation error during transpiling unlike the JavaScript counterpart which is an interpreted language. Developers will be able to catch errors at the early stage during development.
  • TypeScript is optionally strongly typed aka static typed. It also supports type inference.
  • TypeScript support type definitions for existing JavaScript libraries. You may write type definitions in .d.ts file extension.
  • Typescript is a backward compatible version of JavaScript that compiles to pure JavaScript which makes their writing easier and faster. Typescript allows clean, manageable and maintainable code than JavaScript.
  • Plain old JavaScript code may be included in a TypeScript project without any problems since JavaScript is a subset of TypeScript.
  • Typescript compiles down to a version of JavaScript that runs on all browsers.
  • Great community and documentation.
3. Who developed typescript?

Typescript developed and maintained by Microsoft.

4. Drawbacks of typescript.
  • Learning curve.
  • Lack of package manager and dependency on npm.
  • More code to write.
  • Required compilation. Browser cannot read typescript files.
  • Not true static typing.
5. Inbuilt datatypes in typescript.

Data type.Explanation.
number Represents both Integer as well as Floating Point numbers.
string Sequence of characters.
boolean Represents true and false values.
void Used as function return type when it does not return anything.
null Represents variable/object whose value not set.
undefined Represents uninitialized variables.
any Represents variable that hold any value types.
6. What are the user-defined types in typescript?

Besides built-in data types, the user can also define his own data type using classes, interfaces, arrays, Enumerations (enum) and tuple.

7. Difference between Typescript and Javascript.
TypesScript is known as Object oriented programming language.JavaScript is popular as a scripting language.
Typescript supports Classes and interface.JavaScript creates reusable components using functions and prototype based inheritance.
Optional static typing available.Static typing not available.
Typescript allow optional parameters.Javascript does not have that feature.
Typescript generate compilation errors.JavaScript reports error at runtime as it is an interpreted language.
TypeScript is nothing but JavaScript with ES6 features.Javascript does not support ES6.

8. Does typescript support abstract class?

No, typescript supports only class and interface.

9. Does JavaScript support modules like typescript?

No, JavaScript doesn't.

10. How do I install typescript in my computer?

TypeScript can be installed and managed via npm, the Node.js package manager. To install TypeScript, make sure that the npm is installed properly using the below command.

npm -v

To install typescript run the below command.

npm install -g typescript

11. How do I compile typescript files?

Typescript files has the extension .ts. Use tsc <TypeScript File Name> command to compile a typescript file via command line.

To compile multiple typescript files into a single JS file use the below command.

tsc --out some_JS_file.js ts_file1.ts ts_file2.ts ts_file3.ts

12. What are Modules in Typescript?

A module is used to organize code written in TypeScript. Modules are of 2 types, Internal Modules (aka namespaces) and External Modules also referred just as modules simply.

13. Explain interface in typescript.

Interface is a structure that defines the contract/syntax for classes to follow. Classes that are derived from an interface must follow the structure.

An Interface can define properties, methods, and events. Interface only declare the members and deriving class define the members.

interface IEmployee{ 
   emplD: number,
   firstName: string, 
   lastName: string,
   department: string,
   calcGrossIncome: (number)=>number 

TypeScript compiler uses interface for type check and does not convert interface to JavaScript. This is also referred as duck typing or sometimes structural subtyping.

14. Does typescript support all object oriented paradigm?

Yes, typescript supports encapsulation, inheritance, polymorphism and abstraction.

15. How do I compile typescript files automatically with changes to file in real-time?

Using -watch or -w compiler option, we can enable auto compilation.

tsc -watch typescriptFile.ts

This option executes the compiler in watch mode, watch input files, and trigger recompilation on changes.

16. What is class in typescript?

The concept of class is similar to Java, Scala or any other object oriented programming language. A Class can have constructor, member variables, properties and methods. TypeScript also allows access modifiers, private and public for member variables and functions.

17. What is namespace in Typescript?

A namespace is a way to logically group related classes or interfaces in a wrapper.

Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use.

export namespace Shapes {
    export class Triangle { /* ... */ }
    export class Square { /* ... */ }

Namespace is also known as internal modules.

18. What does the question mark (?) on a typescript variable/parameter name denote?

Question mark on a typescript variable marks it as optional. Any optional parameters must follow required parameters.

function sayHello(name: string, salutation?: string) {
    if (salutation)
        return  "Hello " + saluation +"." +  name;
        return "Hello " + name;

The optional parameters will have value as undefined when unused.

19. What is debouncing?

Debouncing is a programming technique that enables us to defer the execution of a potentially-complex operation rather than executing it constantly/immediately.

For example, While filtering large lists based on user input, rather than filtering on every keystroke, we can use debounce functionality to only apply the filter if the user hasn’t typed anything for a few seconds.

20. What is the difference between Subject and BehaviorSubject?

A BehaviorSubject holds one value. When it is subscribed it emits the value immediately. A Subject doesn't hold value.

BehaviourSubject will return the initial value or the current value on Subscription.

var bSubject= new Rx.BehaviorSubject(0);  // 0 is the initial value

  next: (v) => console.log('observerA: ' + v)  // output initial value, then new values on `next` triggers
});;  // output new value 1 for 'observer A';  // output new value 2 for 'observer A', current value 2 for 'Observer B' on subscription

  next: (v) => console.log('observerB: ' + v)  // output current value 2, then new values on `next` triggers

Subject does not return the current value on Subscription. It triggers only on .next(value) call and return/output the value.

var subject = new Rx.Subject();; //Subjects will not output this value

  next: (v) => console.log('observerA: ' + v)
  next: (v) => console.log('observerB: ' + v)
21. Dynamic vs Static Typing.

In dynamically typed languages, the types are associated with run-time values and not names explicitly in your code. Dynamic examples: JavaScript, Python, Ruby, PHP.

In statically typed languages, you explicitly assign types to variables, function parameters, return values, etc. Static examples: Java, C, C++, Rust and Go.

22. What is the Difference Between '!:' and '?:' in TypeScript object definitions?

The ? marks the property optional.

The ! is the definite assignment assertion. It's sort of a declaration-level version of the non-null assertion operator but used on a property (can also be used on variables) rather than on an expression.


Comments & Discussions