Generic types are like functions for type declarations the let you pass some arguments (other types) later. This is a (part of) simple linked list implementation:
type ListNode = {
value: any;
next: ListNode|null;
}
function add(head: ListNode, value: any): ListNode {
const newElement: ListNode = { value, next: null };
let current: ListNode = head;
while (current.next) {
current = current.next;
}
current.next = newElement;
return current;
}
This code is fine, it works, but gives no type safety: