Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
10 Advanced TypeScript Questions For An Interview — With Answers
Check if your candidate is capable of answering these more advanced TypeScript questions1 — What does the ‘Omit’ type do?
Answer: It’s a new type where you can define some properties to be excluded from the original type.
Example:
type Person = { name: string; age: number; location: string; };
type QuantumPerson = Omit<Person, 'location'>; // Same as next line
QuantumPerson = { name: string; age: number; };
2 — When to use the ‘declare’ keyword?
Answer: When using a JavaScript library which doesn’t have declarations, inside your TypeScript project.
Example:
declare const libraryName;
3 — How to get automatically ‘declaration’ files?
Answer: Set the declaration compiler option on true in the tsconfig.json .
Example:
{ "compilerOptions": { ... "declaration": true, }}
4 — How to overload a function?
Answer: Use the same function name again above the original function without the brackets {}. Change the number of arguments, the arguments types or/and the return type.
Example:
function add(x: string, y: string): string;function add(x: number, y: number): number { return x + y;}
5 — How to easily make all properties of an interface optional?
Answer: Use the Partial mapped type.
Example:
interface Person { name: string; age: number;}
type PartialPerson = Partial<Person>; // Same as next lines
interface PartialPerson { name?: string; age?: number;}
6 — Where can decorators be applied to?
Answer: Classes, properties, methods, and method arguments.
Example:
@MyClassDecoratorexport class Person {
... @MyPropertyDecorator myProperty: string;}
7 — What does the ‘Record’ type do?
Answer: It allows you to create a typed map .
Example:
let Person = Record<string, number> = {};Person.age = 25;
8 — How can you access classes outside the module they are defined in?
Answer: Use the export keyword in front of the class.
Example:
export class Person {}
9— When to use the ‘unknown’ keyword?
Answer: When you don’t want to use the any keyword or/and don’t know the exact type up front but want to assign it later on.
Example:
let person: unknown = 'John';
if (typeof person === string) { let name: string = person;}
10—What is a ‘.map’ file, and why/how can you use it?
Answer: The map file is a source map file that can be used when you want to debug. It can be generated by setting the sourceMap compiler option on true in the tsconfig.json .
Example:
{ "compilerOptions": { ... "sourceMap": true, }}
Thank you for reading! My Github. If you found this post useful, consider reading some of my other articles or check out my markdown template generator:
- jeroenouw/cgx
- Angular 8— Share Component Data with Other Components
- When To Use ‘unknown’ and ‘never’ Types in TypeScript
10 Advanced TypeScript Questions For An Interview — With Answers was originally published in HackerNoon.com on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.