Chapter 27: Generics
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Generics
Generics let a function preserve type information across inputs and outputs. A type parameter such as T represents a type supplied or inferred at a call site. This makes reusable helpers type-safe without resorting to any.
How it works
Step-by-step walkthrough
- Define a generic function
- Call with a string
- Call with a number
Example
function first<T>(items: T[]): T | undefined {
return items[0];
}
console.log(first(['QA']), first([42]));Account for empty arrays in return types.
✎ Your practice task
Call first with an empty string array.
Knowledge check
What is T in this example?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.