36.7 Variance Annotations in TypeScript 4.7+: in and out
Right, so you’ve finally decided to get serious about type safety. Good. You’ve probably built a generic class or two, maybe a Box<T> or a Repository<T>, and thought, “This is fine.” And for a while, it is. Then you try to assign a Box<string> to a Box<string | number> and TypeScript screams at you. You stare at the error, a vein throbbing gently in your forehead, because obviously a box of strings should be assignable to a box of strings-or-numbers. What could possibly go wrong?