Courses / TypeScript
18. unknown vs any
Difference between unknown and any and using unknown to safely narrow external values.
1 min read Chapter 18 of 21
JavaScript / TypeScript
unknown vs any
"unknown" forces you to check what a value is before using it; "any" lets you do anything without checks.
"I got a value from the network and I want safety — but I don't want to rewrite everything with casts."
unknown is a type-safe top type that requires you to narrow it before using; any disables type checking and behaves like an escape hatch.
"I got a value from the network and I want safety — but I don't want to rewrite everything with casts."
unknown is a type-safe top type that requires you to narrow it before using; any disables type checking and behaves like an escape hatch.