---
title: "Generics"
excerpt: "Generics provide a way to make functions/components work with any data type and not restrict to one."
date: "2023-06-23"
lastUpdated: "2026-03-24"
tags: ["Generics","TypeScript"]
canonical: "/blog/02-generics"
---

## Example with a class

```typescript
class HoldAnything<T> {
  // T is the name usually given to the generic 'type of data'
  data: T
}

const holdNumber = new HoldAnything<number>()
holdNumber.data = 123
const holdString = new HoldAnything<string>()
holdString.data = 'Hello Wall-e'
```

{/* <!-- truncate --> */}
