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

All topics

TypeScript interview questions and answers for QA

20 TypeScript questions that come up in QA interviews, with short answers. Open the topic in the knowledge base for a deeper explanation.

How does TypeScript differ from JavaScript and which basic types do you know?

TypeScript is a layer over JavaScript with static typing. The tsc compiler checks types at build time and removes them, so plain JS runs at runtime and there is no type checking. Basic types: string, number, boolean, null, undefined, arrays (string[]), tuples, enum, object, and also any (turns checking off), unknown (a safe alternative to any), void and never. In tests this gives autocomplete for page objects and API clients and catches mistakes such as a wrong field name before the run. Types need not be written everywhere: TypeScript infers them from values (type inference).

Read the topic

How does any differ from unknown, and what do you use for an API response?

any turns off type checking completely and spreads further through the code, so errors surface only at runtime. unknown also accepts any value, but you can't use it without a check. For an API response body I take unknown and narrow the type with validation, for example a zod schema through schema.parse. In a test framework it makes sense to forbid any with an ESLint rule.

Read the topic

What is never and how do you apply it in practice?

never is a type with no values, and it is what a function that always throws returns. The most useful application is an exhaustive check in a switch over a union type: in the default branch I assign the value to a variable of type never. If the backend adds a new transaction status, for example REVERSED, and it is added to the type, the code won't compile until I handle the new case. That way a new state doesn't go unnoticed.

Read the topic

How does interface differ from type, and which do you choose?

For describing an object they are almost interchangeable. type can describe unions, tuples, functions, mapped and conditional types, while interface describes only an object's shape, but supports declaration merging and gives clearer errors with extends. I use interface for models and Page Object contracts, and type for unions and utilities. The main thing is one convention in the project.

Read the topic

What is the difference between the field phone?: string and phone: string | undefined? Why does it matter for API tests?

With ? the field may be missing from the object entirely, while with | undefined the field is required, but its value may be undefined. For an API these are different contracts: a field that is always there (JSON has no undefined, so an empty value there is null and the model type is string | null), and a field that is sometimes absent (phone?: string). If the backend started omitting a required field, that is a regression, and my type and schema must catch it. So I describe models exactly by the contract, not in whatever way is more convenient.

Read the topic

What is a discriminated union and where do you use it?

It is a union of types with a common literal field, for example status: 'success' or status: 'error'. When I check this field, TS narrows the whole object to the right branch, and in the success branch data is available, while in the error branch error is. I describe API responses and different payment types this way: card, IBAN, wallet. The advantage over a single type with a pile of optional fields is that invalid combinations simply don't compile.

Read the topic

How do you write your own type guard and what are its risks?

A type guard is a function with a return type value is Account, and after if (isAccount(x)) TS considers x an account. There is also an assertion function with asserts value is T, which throws and narrows the type further down the code. The risk is that the compiler doesn't check the logic inside the guard: if it is incomplete, the type will lie. So for API responses I prefer validation with a schema from which the type is derived.

Read the topic

How would you type the get method in an API client so it returns the right type for each endpoint?

The simplest is a generic get<T>(path): Promise<T>, and the call client.get<Account>(...) gives autocompletion. But that is only an assertion through as T, and nothing is checked at runtime. So in a mature framework I pass a zod schema into the method, and the result type is derived from it with z.infer. That way the type and the real data can't drift apart unnoticed.

Read the topic

Which utility types do you use most often in a test framework?

Partial in test data factories: a base valid object plus overrides: Partial<T>, and the test changes only the important field. Omit to derive a request body type from a model without id and createdAt. Pick for short projections. Record over a union of keys for maps that must cover all variants, for example all currencies.

Read the topic

How do you build a Page Object hierarchy? Do you use inheritance?

I keep at most one level: an abstract BasePage with an open method and an abstract waitForLoaded, which each page implements itself. Deep inheritance chains become brittle, so beyond that I use composition: a page consists of components such as Header or TransferForm. I give pages and clients to tests through Playwright fixtures. I avoid static state in classes, because it is shared between tests in a worker.

Read the topic

How do Promise and async/await work?

A Promise is an object for a result that does not exist yet. It is pending, then fulfilled with a value or rejected with an error; you take the result with .then() and .catch(). An async function always returns a Promise, and await pauses it until the Promise settles, then returns the value or throws the error, which I catch with try/catch. This reads like synchronous code. I run independent operations in parallel with Promise.all, not one by one. The most common mistake in automated tests is a forgotten await: the test moves on without waiting for the step and becomes flaky; the typescript-eslint rule @typescript-eslint/no-floating-promises helps.

Read the topic

What happens if you forget await before expect in Playwright, and how do you protect against it?

A web-first assertion like toBeVisible returns a promise, and without await nobody waits for it. The test may pass green without checking anything, or fail with an error about a closed page, because the test has already finished. I turn on the @typescript-eslint/no-floating-promises rule with a type-aware config. For a synchronous expect(value).toBe(x) await isn't needed, and I keep that difference in mind.

Read the topic

How do Promise.all, allSettled, race and any differ?

Promise.all returns all the results or fails on the first error, and the other promises are not cancelled. allSettled waits for all of them and returns the status of each, and I use it for cleanup so that one error doesn't stop the deletion of the rest of the data. race returns the first one to finish, successful or not, and any returns the first successful one or an AggregateError if all failed. For data preparation in a test I take all.

Read the topic

Why can't you use forEach with an async callback? What is the right way?

forEach ignores the promises the callback returns, so the code after it runs before the operations finish, and errors become unhandled rejections. That way a test may check data that hasn't been cleaned up. If the order matters, I write for...of with await, and if the operations are independent, Promise.all with map. The same goes for filter: an async predicate returns a promise, and it is always truthy.

Read the topic

What are union and literal types? Give an example.

A union type allows one of several types: string | number. A literal type is a type made of one exact value: 'admin' or 200. Together they give a closed set of values: type Role = 'admin' | 'user' | 'guest';. The function login(role: Role) will not accept 'adnim', and the error shows at compile time. In tests I describe environments, browsers, user roles and response statuses this way. Unions work with narrowing (typeof, ===), after which TypeScript knows the exact type in a branch. Unlike an enum, a literal union does not end up in the generated JS.

Read the topic

Which tsconfig options do you always turn on in a test project and why?

strict without fail, because without strictNullChecks and noImplicitAny TS hardly helps. I add noUncheckedIndexedAccess, so that rows[0] has a type with undefined and I don't fail on an empty result. I set noEmit, because Playwright runs the tests, and I configure paths for short imports and list "types": ["node"] explicitly, because since TypeScript 6.0 types is empty by default. Since Playwright doesn't type-check at startup, CI needs a separate tsc --noEmit step.

Read the topic

How do let, const and var differ?

var is function-scoped, is hoisted with the initial value undefined and allows redeclaration, so it causes sneaky bugs. let and const are block-scoped and cannot be accessed before the declaration (temporal dead zone). const forbids reassigning the variable but does not freeze the content: with const user = { name: 'A' } the field user.name can be changed. By default I write const, I use let when a value is really reassigned, and I do not use var at all. A classic example: in for (var i...) all callbacks see the same i, while with let each iteration has its own.

Read the topic

What do optional chaining (?.) and nullish coalescing (??) do and how does ?? differ from ||?

?. reads a nested value safely: user?.address?.city gives undefined if user or address is null or undefined, and does not throw a TypeError. It also works for methods (obj.method?.()) and indexes (arr?.[0]). ?? returns the right operand only when the left one is null or undefined. || reacts to any falsy value: 0, '', false, NaN. So count ?? 10 keeps 0, while count || 10 gives 10. For default values where 0 or an empty string is valid data, I use ??.

Read the topic

Why isn't 0.1 + 0.2 equal to 0.3, and how do you work with money in tests?

number is a binary floating-point number, and 0.1 has no exact representation, so you get 0.30000000000000004. toFixed also rounds inaccurately: (1.005).toFixed(2) gives 1.00. I compare money in minor units as integers, taking into account that JPY has 0 decimal places. Fees and rates I compute with a decimal library using the rounding rule from the specification, for example half-even.

Read the topic

Tell me about the traps with ||, ==, sort and dates that you have seen in tests.

fee || 500 will replace a valid zero fee with 500, so for defaults I use ??. == does a non-obvious coercion, for example '' == 0 is true, so only ===. sort() without a comparator sorts as strings, [10, 1, 2] becomes [1, 10, 2], and it also mutates the array. In Date months are zero-based, and new Date('2026-01-15') is parsed as UTC, so I fix the time zone on CI and in the Playwright config.

Read the topic

Practice TypeScript with flashcards