Enhancing Built-in JavaScript Objects with TypeScript Global Augmentation
This challenge will guide you through the process of augmenting built-in JavaScript objects, such as Array or String, with new methods using TypeScript's declaration merging capabilities. This is a powerful technique for extending the functionality of existing types in a type-safe manner, which can be particularly useful in large projects or when integrating with libraries that don't offer the desired extensions out-of-the-box.
Problem Description
Your task is to implement a new method, sum, for the built-in Array type in TypeScript. This sum method should calculate the sum of all numbers within an array. You will achieve this by using TypeScript's global augmentation feature to add this method to the Array.prototype.
Key Requirements:
- Declare a new method
sumonArray.prototype: This method should be callable on any array instance. - Implement the
summethod: The implementation should iterate through the array and add up its numeric elements. - Handle non-numeric elements: If an element in the array is not a number, it should be ignored in the summation.
- Type safety: Ensure that your augmentation is type-safe, meaning TypeScript can correctly infer the types involved.
Expected Behavior:
- Calling
[1, 2, 3].sum()should return6. - Calling
[10, -5, 0, 2.5].sum()should return7.5. - Calling
[1, 'hello', 2, true, 3].sum()should return6. - Calling
[].sum()should return0.
Edge Cases to Consider:
- Arrays containing mixed data types (numbers, strings, booleans, null, undefined).
- Empty arrays.
- Arrays containing only non-numeric values.
Examples
Example 1:
const numbers1 = [1, 2, 3, 4, 5];
const total1 = numbers1.sum();
// Expected Output: 15
Explanation: The sum method on numbers1 iterates through all elements and returns their sum.
Example 2:
const mixedArray = [10, 'a', 20, false, 30, null, 40];
const total2 = mixedArray.sum();
// Expected Output: 100
Explanation: The sum method ignores non-numeric values ('a', false, null) and sums only the numbers.
Example 3:
const emptyArray: number[] = [];
const total3 = emptyArray.sum();
// Expected Output: 0
Explanation: An empty array should result in a sum of 0.
Constraints
- The augmentation must be done using TypeScript's declaration merging (specifically, augmenting the global
Arrayinterface). - The
summethod should return anumber. - The solution should not rely on any external libraries for the augmentation itself.
- The implementation should be efficient for typical array sizes (e.g., < 1 million elements).
Notes
- To augment a global type like
Array, you'll need to declare adeclare globalblock. - Inside
declare global, you can then declare or merge with existing interfaces/types. - Remember that you'll need to provide both the declaration (type definition) and the runtime implementation. The declaration goes in a
.d.tsfile (or within adeclare globalblock in your code), and the implementation goes in a regular.tsfile. - Think about how to access the array instance within the
summethod.thiswill refer to the array itself. - Consider using
typeoforisNaNto filter out non-numeric values.