Lesson 3 of 9

Lesson 03 — Functions with types

Title: Validation helpers and arrow utilities QA teams reuse

Description: Declare argument and return types for login checks, generators for synthetic users, and small predicates Playwright specs call often.

Why it matters for QA: Untyped helpers hide invalid branches — typed signatures document contracts (boolean gate vs void logger) so reviewers trust shared utilities.


1. Classic function declaration

typescripttypescript
function login(email: string, password: string): boolean {
  return email.length > 0 && password.length > 0;
}

console.log(login("ok@test.dev", "secret")); // true

2. Arrow functions

typescripttypescript
const isValidEmail = (email: string): boolean => {
  return email.includes("@");
};

Single-expression bodies may omit braces:

typescripttypescript
const shortEmailCheck = (email: string): boolean => email.includes("@");

3. Factory-style helpers

typescripttypescript
interface SeedUserInput {
  emailPrefix: string;
}

function createUser(input: SeedUserInput): { email: string; id: number } {
  const email = `${input.emailPrefix}+qa@${Date.now()}.example`;
  const id = Math.floor(Math.random() * 10_000);

  return { email, id };
}

4. Practice tasks

  1. checkPasswordStrength() Create a function that checks password strength.
  2. Write createUser(). The function creates a user, and if no role is given, it automatically sets "guest".