function firstItem(arr: T[]): T | null {
return arr.length ? arr[0] : null;
}
const n = firstItem([1, 2, 3]); // number
const s = firstItem(['a', 'b']); // string PHP has no built-in generics, but PHPStan/Psalm support them via @template annotations and doc blocks. They are checked statically, not at runtime.
/** @template T */
class Collection {
/** @param T $item */
public function add($item): void { ... }
}Collections, repositories, Result types, any container. Java, C#, TypeScript, and Go (since 1.18) have built-in generics.