4. Static Types, Interfaces & Type Aliases

Core TypeScript Types

- Primitives: string, number, boolean - Special: void (functions returning nothing), any (disables type checking - avoid!), unknown (type-safe any)

Union & Literal Types

// Literal union type: only these 3 strings are allowed!
type BrowserName = "chromium" | "firefox" | "webkit";

// Union type:
type LocatorTarget = string | number;

Interfaces vs Type Aliases

In automation frameworks, we use interfaces for object shapes (test configs, page models):
interface TestConfig {
  baseUrl: string;
  timeout?: number;        // Optional property (indicated by ?)
  readonly retries: number; // Cannot be reassigned after creation
}

Type Assertions (as)

When you know more about a value's type than TypeScript does:
const responseData = (rawJson as { token: string }).token;

Task

  • Define an interface `BrowserSession` with:
  • `browser`: of type `"chromium" | "firefox" | "webkit"`
  • `headless`: boolean
  • `viewportWidth?`: optional number
πŸ’‘Common doubts
Did you like the problem?
Lesson 4 of 36
TypeScript
Loading code editor…
Next