MediumJavaScriptTypeScript

Function Overloads

TypeScriptTypesFunctions

TypeScript's function overloads let you declare multiple call signatures for a single function, giving callers precise types per input:

function double(value: string):  string;
function double(value: number):  number;
function double(value: boolean): boolean;
// Implementation (not visible to callers):
function double(value: string | number | boolean): string | number | boolean {
  ...
}

const s = double('hi');   // TypeScript infers: string ✅
const n = double(5);      // TypeScript infers: number ✅
const b = double(true);   // TypeScript infers: boolean ✅

Without overloads, double would return string | number | boolean for every call — callers would always need to narrow.

Implement double

Input typeBehaviorExample
stringRepeat twice'hi''hihi'
numberMultiply by 2510
booleanNegatetruefalse
string[]Repeat each element twice['a','b']['aa','bb']

The TypeScript challenge: declare four overload signatures so each call site gets the exact return type.

Sample tests

Test #1string is repeated twice
Input: ["hi"]
Output: "hihi"
Test #2empty string repeated is still empty
Input: [""]
Output: ""
Test #3number is doubled
Input: [5]
Output: 10
Test #40 doubled is 0
Input: [0]
Output: 0
Test #5negative number is doubled
Input: [-3]
Output: -6
Test #6true negated is false
Input: [true]
Output: false
Test #7false negated is true
Input: [false]
Output: true
Test #8string array: each element repeated
Input: [["a","b"]]
Output: ["aa","bb"]