avatarDaniel Anderson

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4096

Abstract

pan class="hljs-keyword">super</span>(); <span class="hljs-keyword">this</span>.name = name; <span class="hljs-keyword">super</span>.doSomething(); } }</pre></div><ul><li><b>What is namespace in Typescript?</b></li></ul><p id="095e">A namespace is simply a way to logically group related classes or interfaces in a wrapper.</p><p id="bee0"><i>Example:</i></p><div id="7576"><pre><span class="hljs-keyword">namespace</span> <span class="hljs-symbol">Transport</span> { export <span class="hljs-keyword">class</span> <span class="hljs-symbol">Car</span> { }
export <span class="hljs-keyword">class</span> <span class="hljs-symbol">Bike</span> { }
}</pre></div><ul><li><b>What is default visibility for properties/methods in Typescript classes?</b></li></ul><p id="c8a7"><code>public</code> is the default</p><ul><li><b>What are all the other access modifiers that TypeScript supports?</b></li></ul><p id="ad50"><code>public</code> - All the members of the class, its child classes, and the instance of the class can access.</p><p id="0942"><code>protected</code> - All the members of the class and its child classes can access them. But the instance of the class can not access.</p><p id="e1b4"><code>private</code> - Only the members of the class can access them.</p><ul><li><b>How do you define an optional property in TypeScript?</b></li></ul><p id="9804">You're looking for with <code>?</code> or <code>| undefined</code></p><p id="e277"><i>Example :</i></p><div id="cbc2"><pre><span class="hljs-comment">// Example 1</span> <span class="hljs-keyword">interface</span> <span class="hljs-symbol">ISomeInterface</span> { foo?: <span class="hljs-built_in">string</span>; }</pre></div><div id="1a3d"><pre><span class="hljs-comment">// Example 2</span> <span class="hljs-keyword">interface</span> <span class="hljs-symbol">ISomeInterface</span> { foo: <span class="hljs-built_in">string</span> | undefined; }</pre></div><ul><li><b>What are decorators in TypeScript?</b></li></ul><p id="bfff">Decorators are simply functions that modify a class, property, method, or method parameter.</p><ul><li><b>How do you declare a decorator?</b></li></ul><p id="f6c7">The syntax is an “@” symbol followed by a function.</p><p id="6479"><i>Example:</i></p><div id="14c7"><pre>@readonly <span class="hljs-keyword">class</span> <span class="hljs-symbol">foo</span> { name?: <span class="hljs-built_in">string</span>; }</pre></div><ul><li><b>What is function Overloading in TypeScript?</b></li></ul><p id="a830">Function overloading is the ability to create multiple functions of the same name with different implementations.</p><p id="01fb"><i>Example:</i></p><div id="2a52"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">Account</span> { <span class="hljs-title class_">Id</span>: <span class="hljs-built_in">number</span>;

<span class="hljs-title function_">search</span>(<span class="hljs-title class_">Id</span>: <span class="hljs-built_in">number</span>);
<span class="hljs-title function_">search</span>(<span class="hljs-attr">name</span>:<span class="hljs-built_in">string</span>);
<span class="hljs-title function_">search</span>(<span class="hljs-params">value: <span class="hljs-built_in">any</span></span>) {
<span class="hljs-keyword">if</span> (value &amp;&amp; <span class="hljs-keyword">typeof</span> value == <span class="hljs-string">"number"</span>) {
    <span class="hljs-comment">//Do something</span>
}
<span class="hljs-keyword">if</span> (value &amp;&amp; <span class="hljs-keyword">typeof</span> value == <span class="hljs-string">"string"</span>) {
    <span class="hljs-comment">//Do Something</span>
}

} }</pre></div><ul><li><b>Can you access static methods in TypeScript?</b></li></ul><p id="f8ca">Yes, you can!</p><p id="ee00"><i>Example</i></p><div id="c32d"><pre><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span> {

<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-title">Hello</span>()</span> { 
}

}</

Options

pre></div><div id="0fa7"><pre>import { Foo } from <span class="hljs-string">"path/Foo"</span><span class="hljs-comment">;</span> Foo.Hello()<span class="hljs-comment">;</span></pre></div><ul><li><b>What use is a constructor in TypeScript?</b></li></ul><p id="f76c">A constructor is responsible for initializing the properties/variable of the class.</p><p id="c61c"><i>Example</i></p><div id="04a3"><pre><span class="hljs-built_in">class</span> Foo { <span class="hljs-built_in">name</span> = <span class="hljs-string">""</span>; <span class="hljs-built_in">id</span>?: <span class="hljs-built_in">number</span>;</pre></div><div id="1bc4"><pre> <span class="hljs-function"><span class="hljs-keyword">constructor</span><span class="hljs-params">(id?: number, <span class="hljs-keyword">name</span>: <span class="hljs-keyword">string</span>)</span> <span class="hljs-comment">{ this.id = id; this.name = name; }</span> }</span></pre></div><ul><li><b>What is the<code>as</code> keyword used for in TypeScript?</b></li></ul><p id="493e">The <code>as</code> is additional syntax for Type assertion in TypeScript.</p><p id="81bc"><i>Example:</i></p><div id="0812"><pre>let <span class="hljs-keyword">user</span> <span class="hljs-title">= formData</span> as User;</pre></div><ul><li><b>What is generics in TypeScript?</b></li></ul><p id="e9e3">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</p><p id="8769">A generic class/interface can be defined using <code><T></code></p><p id="91e7"><i>Example:</i></p><div id="a5b9"><pre><span class="hljs-keyword">function</span> <span class="hljs-title">someType<T></span>(anything: T): T {
<span class="hljs-keyword">return</span> <span class="hljs-type">anything</span>; }</pre></div><div id="4ccc"><pre><span class="hljs-keyword">let</span> myOutputString = someType<<span class="hljs-built_in">string</span>>(<span class="hljs-string">"myString"</span>); <span class="hljs-comment">// type of myOutputString variable will be 'string'</span></pre></div><div id="78b7"><pre>let myOutputNumber = someType<number>(<span class="hljs-number">12</span>); // <span class="hljs-keyword">type</span> <span class="hljs-type">of </span>myOutputNumber variable will be <span class="hljs-symbol">'number</span>'</pre></div><h1 id="5025">Conclusion</h1><p id="d89f">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.</p><p id="1803">I think questions alone are not the best way to go about interviews. Other ideas to go along with questions:</p><ul><li>Coding challenges, like the Fizz Buzz test maybe not this exact one as I feel everyone has heard of this!</li><li>Give them a task(s) beforehand<b> </b>to code and then explain to you what they have done in the interview</li><li>Explain a project they have worked on as part of a team or individually</li></ul><div id="49ee" class="link-block"> <a href="https://readmedium.com/24-quick-fire-javascript-interview-questions-a71f78d03f08"> <div> <div> <h2>24 quick-fire JavaScript interview questions</h2> <div><h3>JavaScript Quickfire interview questions</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*yn26hsqcdIDMAA5H)"></div> </div> </div> </a> </div><h1 id="3cc0">A note from In Plain English</h1><p id="faaa">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 <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>clicking here</b></a>, and be sure to subscribe to the channel 😎</p></article></body>

Top 19 frequently asked TypeScript interview questions

Common interview questions that get asked about TypeScript

Photo by Sebastian Herrmann on Unsplash

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.json file?

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 theas keyword 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 😎

JavaScript
Typescript
Programming
Software Development
Coding
Recommended from ReadMedium