QA Interview Hub · Knowledge base · Quizzes · Українська
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
let a = 'hi';
const b = 'hi';const arr = [1, 'a', 2];const cfg = { env: 'prod', retries: 2 } as const;function f(timeout?: number) {
// timeout: ?
}const retries: number | undefined = 0;
const n = retries ?? 5;
const m = retries || 5;A | B different from the intersection A & B for object types? Juniorconst different from readonly? Juniorfunction greet(count: number | undefined) {
if (count) return 'Count: ' + count;
return 'No count';
}
greet(0);a?.b?.() do? Juniorconfig.retries ??= 2 do? JuniorTake the quiz for this chapter · Read the explanation in the knowledge base
type User = { name: string };
const extra = { name: 'Ann', age: 30 };
const a: User = extra; // (1)
const b: User = { name: 'Ann', age: 30 }; // (2)interface Cfg { readonly url: string }
const c: Cfg = { url: 'a' };
c.url = 'b';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
function f(x: string[] | null) {
if (typeof x === 'object') {
// x: ?
}
}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;
}
}function isFish(pet: Fish | Bird): pet is Fish {
return 'swim' in pet;
}type Ok = { data: string };
type Err = { error: string };
function h(r: Ok | Err) { /* ? */ }const routes = { home: '/', login: '/login' } satisfies Record<string, string>;const user = JSON.parse(body) as User;try { await api.call(); }
catch (e) {
// e: unknown
}function assertDefined<T>(v: T, msg: string): asserts v is NonNullable<T> {
if (v == null) throw new Error(msg);
}instanceof? MiddleTake the quiz for this chapter · Read the explanation in the knowledge base
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
const x = first([10, 20]);function len<T extends { length: number }>(v: T) {
return v.length;
}function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const v = get({ id: 1, name: 'Ann' }, 'name');async function load() {
return 42;
}function wrap<T>(v: T): T {
return { value: v };
}Take the quiz for this chapter · Read the explanation in the knowledge base
type T = Exclude<'a' | 'b' | 'c', 'a'>;function buildUser() {
return { id: 1, roles: ['admin'] };
}type Cfg = Readonly<{ user: { name: string } }>;
const c: Cfg = { user: { name: 'a' } };
c.user.name = 'b'; // (1)
c.user = { name: 'b' }; // (2)type Status = 'active' | 'blocked';
type Counters = Record<Status, number>;Take the quiz for this chapter · Read the explanation in the knowledge base
class LoginPage {
constructor(private readonly page: Page) {}
}class Counter {
count = 0;
inc() { this.count++; }
}
const c = new Counter();
const fn = c.inc;
fn();class ApiClient {
token: string;
async init() { this.token = await login(); }
}interface Page { open(url: string): Promise<void> }
class LoginPage implements Page {
async open(url) { /* ... */ }
}static field different from an ordinary one? Middleclass 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
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);async function cleanup(ids: number[]) {
ids.forEach(async id => {
await api.delete(id);
});
console.log('done');
}async function f() {
try {
return Promise.reject(new Error('x'));
} catch {
return 'caught';
}
}
const r = await f().catch(() => 'outer');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
const STATUS = { ok: 'OK', fail: 'FAILED' } as const;
type Status = ???;Take the quiz for this chapter · Read the explanation in the knowledge base
.d.ts file contain? Juniorpaths in the compiled code? Middle'admin' be assigned to a variable of the string enum type Role? Middleenum Role { Admin = 'admin', Viewer = 'viewer' }
const r: Role = 'admin';noEmit: true useful on the CI of a test project? JuniorTake the quiz for this chapter · Read the explanation in the knowledge base