How to extend Array.prototype in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Extend Array.prototype in TypeScript

To extend Array.prototype in TypeScript:

  1. Create a .d.ts file and add an interface that extends Array.
  2. Add types for the methods you intend to use on array objects.
  3. Add the methods to the Array.prototype object.

In the src directory of your project, create a types directory containing the following index.d.ts file.

src/types/index.d.ts
export {}; declare global { interface Array<T> { removeLast(): T[]; } }
The code for this article is available on GitHub

The example shows how to extend the Array interface with a method named removeLast() that returns an array containing elements of the same type.

This will be different in your case, so make sure to adjust the method names and types.

Now in your index.ts file, add the method to the Array.prototype object.

index.ts
const arr = ['a', 'b', 'c', 'd']; if (!Array.prototype.removeLast) { Array.prototype.removeLast = function () { this.pop(); return this; }; } // 👇️ const result: string[] const result = arr.removeLast(); console.log(arr); // 👉️ ['a', 'b', 'c']

add method to array prototype

The code for this article is available on GitHub

We added the removeLast method to the Array.prototype and used it.

Note that you have to add the method to the Array.prototype object in a file that runs before all other files, e.g. an index.ts file.

If you try to use the method before it was added to the prototype you'd get an error.

# Add the path to your types directory to your tsconfig.json file

If you are getting an error in your IDE, try adding the path to your types directory to your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { // ... rest "typeRoots": ["./node_modules/@types", "./src/types"] } }

We used the export {} line in our index.d.ts file to mark it as an external module.

A module is a file that contains at least 1 import or export statement. We are required to do that to be able to augment the global scope.

Note that you will have to change the contents of the provided index.d.ts file according to your use case.

You should add the names (and types) of all of the methods you intend to access on arrays.

src/types/index.d.ts
export {}; declare global { interface Array<T> { removeLast(): T[]; } }

The provided file simply adds a removeLast method with a return type of T[], which is most likely not what you need.

We used a generic in the method type. Generics are like variables but for types.

This allows us to set a flexible type for the return type of the method. We are basically telling TypeScript that the removeLast method takes no parameters and returns an array containing elements of the same type.

TypeScript looks for .d.ts files in the same places it looks for your regular .ts files.

The location is determined by the include and exclude settings in your tsconfig.json file.

TypeScript will merge the declared from you Array interface with the original Array interface, so when you use the arrays, you will be able to access methods and properties from both interfaces.

I've also written an article on how to get the type of the array elements from an array type.

If you need to extend String.prototype, Object.prototype, Date.prototype or Number.prototype, click on the link and follow the instructions.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.