Top 19 frequently asked TypeScript interview questions
Common interview questions that get asked about TypeScript
Introduction
TypeScript is everywhere on the web these days so if you're a front developer or even a full stack developer it's important to know the key concepts of TypeScript. Some of the below questions probably could be used for other OOP languages like c# or Java.
The below questions can be used for all levels. When interviewing I usually give the same test to all the different levels as this can give you a baseline to what level you think the developer is.
TypeScript interview questions
- What are some advantages of using TypeScript?
One or many of the below you should be looking for.
- Static typing
- New Features + Browser Compatibility
- Intellisense
- Type annotations
- Readability
- Fast refactoring
- What is the use of the
tsconfig.jsonfile?
The tsconfig.json file specifies the root files and the compiler options required to compile the project.
- How do you declare a variable in TypeScript?
You’re looking for of the below in the example.
Example:
let name = "Dave"; // TypeScript will automaically create this as a string;let name: string = "Dave" // Using ":"- Which keyword is used for inheritance in TypeScript?
extends is the keyword you should be looking for.
Example:
class dog extends IAnimal- In TypeScript can you inherit off a class?
Yes, you can use the keyword extends
Example:
class Animal {
numberOfLegs = 0;
}class dog extends Animal- Can you in inherit from more than one class?
No, you can’t in TypeScript.
- Is there anything that can help you with this?
mixins create partial classes which we can combine to form a single class that contains all the methods and properties from the partial classes.
- What is the super keyword in TypeScript?
Super is a TypeScript keyword which can be used by developers in expressions for base class constructor and base class properties reference.
Example:
class Dog extends Animal {
constructor(name) {
super();
this.name = name;
super.doSomething();
}
}- What is namespace in Typescript?
A namespace is simply a way to logically group related classes or interfaces in a wrapper.
Example:
namespace Transport {
export class Car { }
export class Bike { }
}- What is default visibility for properties/methods in Typescript classes?
public is the default
- What are all the other access modifiers that TypeScript supports?
public - All the members of the class, its child classes, and the instance of the class can access.
protected - All the members of the class and its child classes can access them. But the instance of the class can not access.
private - Only the members of the class can access them.
- How do you define an optional property in TypeScript?
You're looking for with ? or | undefined
Example :
// Example 1
interface ISomeInterface {
foo?: string;
}// Example 2
interface ISomeInterface {
foo: string | undefined;
}- What are decorators in TypeScript?
Decorators are simply functions that modify a class, property, method, or method parameter.
- How do you declare a decorator?
The syntax is an “@” symbol followed by a function.
Example:
@readonly
class foo {
name?: string;
}- What is function Overloading in TypeScript?
Function overloading is the ability to create multiple functions of the same name with different implementations.
Example:
class Account {
Id: number;
search(Id: number);
search(name:string);
search(value: any) {
if (value && typeof value == "number") {
//Do something
}
if (value && typeof value == "string") {
//Do Something
}
}
}- Can you access static methods in TypeScript?
Yes, you can!
Example
class Foo {
static Hello() {
}
}import { Foo } from "path/Foo";
Foo.Hello();- What use is a constructor in TypeScript?
A constructor is responsible for initializing the properties/variable of the class.
Example
class Foo {
name = "";
id?: number; constructor(id?: number, name: string) {
this.id = id;
this.name = name;
}
}- What is the
askeyword used for in TypeScript?
The as is additional syntax for Type assertion in TypeScript.
Example:
let user = formData as User;- What is generics in TypeScript?
Enables a developer to create a component that can work over a variety of types rather than a single one. This allows developers to consume these components and use their own types
A generic class/interface can be defined using <T>
Example:
function someType<T>(anything: T): T {
return anything;
}let myOutputString = someType<string>("myString"); // type of myOutputString variable will be 'string'let myOutputNumber = someType<number>(12); // type of myOutputNumber variable will be 'number'Conclusion
I think the questions above give a well-rounded TypeScript interview and allows the developer to show of their skills. With some questions being very specific on TypeScript and other questions focusing on OOP principles.
I think questions alone are not the best way to go about interviews. Other ideas to go along with questions:
- Coding challenges, like the Fizz Buzz test maybe not this exact one as I feel everyone has heard of this!
- Give them a task(s) beforehand to code and then explain to you what they have done in the interview
- Explain a project they have worked on as part of a team or individually
A note from In Plain English
Did you know that we have launched a YouTube channel? Every video we make will aim to teach you something new. Check us out by clicking here, and be sure to subscribe to the channel 😎
