Programming for Automation
TypeScript Interview Questions & Answers
TypeScript interview questions for automation testers: types vs interfaces, generics, utility types, type guards, tsconfig and typing Playwright page objects and fixtures.
Interview Questions
Showing 15 of 50 questions
TypeScript adds static types to JavaScript, catching typos, wrong arguments and missing awaits at compile time instead of during a long test run. It also gives strong autocompletion for page objects, fixtures and API payloads.
Primitives are string, number, boolean, bigint, symbol, null and undefined, plus arrays, tuples, objects, and special types any, unknown, void and never. Type inference often lets you skip explicit annotations.
Both describe object shapes. Interfaces can be extended and merged through declaration merging, while type aliases can also represent unions, tuples, primitives and mapped types.
any disables type checking entirely, while unknown accepts any value but forces you to narrow it before use. Prefer unknown for API responses and caught errors.
A union (A | B) means a value can be one of several types, while an intersection (A & B) combines all properties of multiple types into one. Unions model options, intersections compose shapes.
Enums define a named set of constants and emit a runtime object. Many teams now prefer string literal unions or as const objects because they are erasable, tree-shakable and work with Node's type stripping.
? marks a property or parameter as possibly undefined, readonly prevents reassignment after creation, and ! tells the compiler a value is not null or undefined. Use ! sparingly because it bypasses safety.
Generics let functions, classes and types work with many types while preserving type information. In automation they type API clients, test data builders and helper utilities.
Partial, Required, Pick, Omit, Record, Readonly, ReturnType and Awaited are the most common. They derive new types from existing ones instead of duplicating definitions.
Narrowing refines a broad type to a specific one using checks like typeof, instanceof, in, or equality on a discriminant. Custom type guards are functions returning value is Type.
strict: true turns on a family of checks including strictNullChecks, noImplicitAny, strictFunctionTypes and useUnknownInCatchVariables. It should be on for any new automation project.
Key options are target, module, moduleResolution, strict, baseUrl/paths for aliases, and types. Playwright transpiles tests itself, so tsconfig mainly drives editor checks and tsc --noEmit in CI.
Import Page and Locator types, declare locators as readonly properties, and initialise them in the constructor. Methods return Promise<void> or typed values so tests get autocompletion.
Call test.extend with a generic describing your fixture types, then provide each fixture with an async function that calls use(). Tests then receive fully typed fixtures through destructuring.
keyof produces a union of an object type's keys, typeof extracts a type from a value, and mapped types iterate over keys to build new types. Together they derive types from runtime config.