class
Box<T>
generic
choose the type parameter T
Box<number>
Box<string>
Box<User>
new
Box
<
number
>()
value to push
box.push(value)
contrast: drop type safety
Use raw
Box<any>
A raw box accepts
anything
— no compile check. The bug surfaces later, at runtime.
The box
1 item
← typed: errors caught early
Box
<
number
>
peek()
pop()
use item — .toFixed()
console
Reset
▸
A
typed
box rejects the wrong type
before it runs
. The
raw
box accepts anything, then crashes at
runtime
. Generics move the error earlier.