Typed Tuple [string, number]
Write a TypeScript function that takes a tuple [string, number] representing a person (name, age) and returns a formatted string like "Name (age)". Tuples in TypeScript are arrays with fixed lengths and typed elements at each position.
Tuple types allow specifying the type of each element at a specific position: [string, number] means element 0 must be a string and element 1 must be a number. This provides more precision than a plain array type string[] or (string | number)[]. Accessing tuple elements returns the correct type at each index.
TypeScript also supports optional tuple elements, rest elements in tuples, and labeled tuples for documentation. Tuples are commonly used for function return types (returning multiple values), CSV row representation, and coordinate pairs where each position has a specific meaning.
Time complexity is O(1) for accessing tuple elements by index. Space complexity is O(1). Tuple types are a compile-time concept that compiles to regular JavaScript arrays with no runtime overhead.
Edge cases include accessing out-of-bounds indices (caught by the compiler for fixed-length tuples), tuples with optional elements, and ensuring the return type matches the expected string format.
Example Input & Output
Another format
Younger person
Another person
Format name and age
Another example
Algorithm Flow
![Recommendation Algorithm Flow for Typed Tuple [string, number]](/flowcharts/tuple-typed.png)
Solution Approach
Best Answers
function solution(person: [string, number]): string {
return person[0] + ' (' + person[1] + ')';
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
