How do you type a function that can return different types based on its input parameter in TypeScript?
Instruction: Provide an example of a function implementation that uses conditional types based on its inputs.
Context: Assesses the candidate's ability to use TypeScript's advanced types, specifically conditional types, to create highly dynamic and type-safe functions.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
A conditional type can describe how a result type depends on its input type. The runtime still needs a branch that implements that relationship. Here a string is converted to a finite number, while a finite number becomes a string.
type Converted<T extends string | number> = T extends string ?...