Playwright + TS · Lesson 5 of 36
Module 5: TypeScript Functions
Your task
Create a function retryAction with:
actionName of type stringmaxAttempts of type number with default value 3string that returns "${actionName} configured with ${maxAttempts} attempts".Write it in the editor, then press Check answer (Ctrl + Enter).
// Explicit parameter types and return type:
function calculateTimeout(retries: number, baseTimeout: number = 5000): number {
return baseTimeout * (retries + 1);
}
// Optional parameter (?):
function logResult(testName: string, durationMs?: number): void {
if (durationMs !== undefined) {
console.log(`${testName} finished in ${durationMs}ms`);
} else {
console.log(`${testName} finished`);
}
}
// Async function return signature: always Promise<T>
async function fetchToken(apiUrl: string): Promise<string> {
return "auth_token_xyz";
}Now that you know basic TypeScript types, let's look at functions in TypeScript. We will learn how to define parameter types, return types, optional and default parameters, and strongly typed callbacks. This gives you complete confidence when building reusable automation utility functions. Let's get started!
Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
JavaScript async functions automatically wrap whatever you return inside a Promise. In TypeScript, the return type must be written as Promise<string>.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___