QA Interview Hub · Knowledge base · Quizzes · Українська

All topics

TypeScript interview questions

62 questions on TypeScript, from the basics to the hard ones. Answers and explanations are in the quiz and the knowledge base.

By level: Junior QA interview questions · Middle QA interview questions · Senior QA interview questions

Basics: primitives, any, unknown, never, literals

  1. What happens to TypeScript types after compilation? Junior
  2. How is unknown different from any? Junior
  3. Which types will TypeScript infer? Junior
    let a = 'hi';
    const b = 'hi';
  4. What is the type of the array? Junior
    const arr = [1, 'a', 2];
  5. What does as const do for an object? Junior
    const cfg = { env: 'prod', retries: 2 } as const;
  6. When is the never type used? Junior
  7. strict is on in tsconfig. What is the type of the parameter inside the function? Junior
    function f(timeout?: number) {
      // timeout: ?
    }
  8. What are n and m? Junior
    const retries: number | undefined = 0;
    const n = retries ?? 5;
    const m = retries || 5;
  9. What is the type of process.env.BASE_URL in a Node project with @types/node? Junior
  10. What does the ! operator after an expression do: document.querySelector('#total')! or process.env.USER!? Junior
  11. How is the union A | B different from the intersection A & B for object types? Junior
  12. How is const different from readonly? Junior
  13. What will the code output, and what is wrong with it? Middle
    function greet(count: number | undefined) {
      if (count) return 'Count: ' + count;
      return 'No count';
    }
    greet(0);
  14. What does a?.b?.() do? Junior
  15. What does config.retries ??= 2 do? Junior

Take the quiz for this chapter · Read the explanation in the knowledge base

Object types: interface and type, optional, readonly

  1. What can an interface do that a type alias can't? Junior
  2. Which line gives a compile error? Middle
    type User = { name: string };
    const extra = { name: 'Ann', age: 30 };
    const a: User = extra;               // (1)
    const b: User = { name: 'Ann', age: 30 }; // (2)
  3. What happens? Junior
    interface Cfg { readonly url: string }
    const c: Cfg = { url: 'a' };
    c.url = 'b';
  4. Is this assignment allowed? Middle
    class Point { x = 0; y = 0 }
    const p: Point = { x: 1, y: 2 };

Take the quiz for this chapter · Read the explanation in the knowledge base

Type narrowing and type guards

  1. What is the type of x inside the if? Middle
    function f(x: string[] | null) {
      if (typeof x === 'object') {
        // x: ?
      }
    }
  2. Why is the line with never here? Middle
    type Shape = { kind: 'circle'; r: number } | { kind: 'square'; a: number };
    function area(s: Shape) {
      switch (s.kind) {
        case 'circle': return Math.PI * s.r ** 2;
        case 'square': return s.a ** 2;
        default: const _x: never = s; return _x;
      }
    }
  3. What does the return type pet is Fish mean? Middle
    function isFish(pet: Fish | Bird): pet is Fish {
      return 'swim' in pet;
    }
  4. How do you narrow a union whose members differ by the presence of a field? Middle
    type Ok = { data: string };
    type Err = { error: string };
    function h(r: Ok | Err) { /* ? */ }
  5. How is satisfies different from a type annotation? Middle
    const routes = { home: '/', login: '/login' } satisfies Record<string, string>;
  6. Why is as dangerous? Middle
    const user = JSON.parse(body) as User;
  7. How do you narrow the error type in a catch? Junior
    try { await api.call(); }
    catch (e) {
      // e: unknown
    }
  8. What is an assertion function? Middle
    function assertDefined<T>(v: T, msg: string): asserts v is NonNullable<T> {
      if (v == null) throw new Error(msg);
    }
  9. How does TypeScript narrow a type with instanceof? Middle

Take the quiz for this chapter · Read the explanation in the knowledge base

Generics

  1. What is the type of x? Middle
    function first<T>(arr: T[]): T | undefined {
      return arr[0];
    }
    const x = first([10, 20]);
  2. What does the extends constraint do? Middle
    function len<T extends { length: number }>(v: T) {
      return v.length;
    }
  3. What is the type of v? Middle
    function get<T, K extends keyof T>(obj: T, key: K): T[K] {
      return obj[key];
    }
    const v = get({ id: 1, name: 'Ann' }, 'name');
  4. What type does the async function return? Junior
    async function load() {
      return 42;
    }
  5. Why is this an error? Middle
    function wrap<T>(v: T): T {
      return { value: v };
    }
  6. How do you type a function that returns the result of any async helper without losing the type? Middle

Take the quiz for this chapter · Read the explanation in the knowledge base

Utility types

  1. What does Partial<User> do? Junior
  2. How do you get the User type without the password field? Junior
  3. What is the type? Middle
    type T = Exclude<'a' | 'b' | 'c', 'a'>;
  4. How do you get a function's result type without duplicating it? Middle
    function buildUser() {
      return { id: 1, roles: ['admin'] };
    }
  5. What happens? Middle
    type Cfg = Readonly<{ user: { name: string } }>;
    const c: Cfg = { user: { name: 'a' } };
    c.user.name = 'b'; // (1)
    c.user = { name: 'b' }; // (2)
  6. What does this type describe? Middle
    type Status = 'active' | 'blocked';
    type Counters = Record<Status, number>;

Take the quiz for this chapter · Read the explanation in the knowledge base

Classes and access modifiers

  1. How is private different from #field? Middle
  2. What does this constructor do? Middle
    class LoginPage {
      constructor(private readonly page: Page) {}
    }
  3. What can you do with abstract class BasePage? Middle
  4. What happens when fn() is called? Middle
    class Counter {
      count = 0;
      inc() { this.count++; }
    }
    const c = new Counter();
    const fn = c.inc;
    fn();
  5. Why is this an error in strict mode? Middle
    class ApiClient {
      token: string;
      async init() { this.token = await login(); }
    }
  6. What is wrong here? Middle
    interface Page { open(url: string): Promise<void> }
    class LoginPage implements Page {
      async open(url) { /* ... */ }
    }
  7. How is a static field different from an ordinary one? Middle
  8. What does the get accessor do in the Page Object class? Middle
    class CartPage {
      constructor(private page: Page) {}
      get total() { return this.page.getByTestId('total'); }
    }

Take the quiz for this chapter · Read the explanation in the knowledge base

Asynchrony: Promise, async/await, parallelism

  1. What will the code output? Middle
    console.log(1);
    setTimeout(() => console.log(2), 0);
    Promise.resolve().then(() => console.log(3));
    console.log(4);
  2. How is Promise.all different from Promise.allSettled? Middle
  3. What is wrong here? Middle
    async function cleanup(ids: number[]) {
      ids.forEach(async id => {
        await api.delete(id);
      });
      console.log('done');
    }
  4. What is r? Middle
    async function f() {
      try {
        return Promise.reject(new Error('x'));
      } catch {
        return 'caught';
      }
    }
    const r = await f().catch(() => 'outer');
  5. Why is 0.1 + 0.2 === 0.3 false, and what do you do with money in tests? Middle
  6. What is the return type of a function marked async that has no return? Middle
  7. What happens to the error here? Middle
    async function save() { throw new Error('db down'); }
    function handler() { save(); return 'ok'; }

Take the quiz for this chapter · Read the explanation in the knowledge base

Advanced types: keyof, typeof, mapped, conditional, template literal

  1. Why write import type { Page } from '@playwright/test'? Middle
  2. How do you get the type of a constant object's values? Middle
    const STATUS = { ok: 'OK', fail: 'FAILED' } as const;
    type Status = ???;

Take the quiz for this chapter · Read the explanation in the knowledge base

Modules and tsconfig

  1. What does a .d.ts file contain? Junior
  2. Does TypeScript rewrite paths from paths in the compiled code? Middle
  3. Can the string 'admin' be assigned to a variable of the string enum type Role? Middle
    enum Role { Admin = 'admin', Viewer = 'viewer' }
    const r: Role = 'admin';
  4. Why is noEmit: true useful on the CI of a test project? Junior
  5. How is a named export different from a default export in practice? Middle

Take the quiz for this chapter · Read the explanation in the knowledge base