How to properly use TypeScript's 'unknown' type in function parameters?
Instruction: Discuss the benefits and use cases of the 'unknown' type, especially as function parameters, including code examples.
Context: This question assesses the candidate's knowledge of the 'unknown' type as a safer alternative to 'any', focusing on its use in enhancing function type safety and robustness.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
An unknown parameter accepts any value while requiring narrowing before type-specific use. It is useful for external JSON, caught errors or an integration whose runtime shape is not yet trusted. You can still pass it through or compare it; unknown does not mean every operation is forbidden.
type Message = { text: string }; function parseMessage(input: unknown): Message { if (typeof input !== "object" || input === null || Array.isArray(input) || !("text" in input) || typeof input.text !==...