96

How to add a method to a base type, say Array? In the global module this will be recognized

interface Array {
   remove(o): Array;
}

but where to put the actual implementation?

0

6 Answers 6

154

You can use the prototype to extend Array:

interface Array<T> {
    remove(o: T): Array<T>;
}

Array.prototype.remove = function (o) {
    // code to remove "o"
    return this;
}

If you are within a module, you will need to make it clear that you are referring to the global Array<T>, not creating a local Array<T> interface within your module:

declare global {
    interface Array<T> {
        remove(o: T): Array<T>;
    }
}
7
  • 1
    @FrancoisVanderseypen that could be a pain, I suggest you don't try it. It is easier the way proposed here. But if you are curious: stackoverflow.com/a/14001136/340760
    – BrunoLM
    Dec 22, 2013 at 19:41
  • it should be interface Array<T> { remove(o): T[]; } in new version with generics Feb 9, 2014 at 0:04
  • @SteveFenton Hmm, can you elaborate? I can do such things in regular Node modules, why TypeScript forbids that? Do you know any workarounds?
    – Gill Bates
    Sep 14, 2015 at 7:40
  • 1
    Definitions must be at the same level in order to take effect, so if you are in a module and you declare interface Array<T> that is a member of the module, i.e. MyModule.Array<T>. That means the global Array<T> is not extended, but a new local interface is created. You have to put extensions in the global scope... I'd suggest putting the interface in a .d.ts file. Do you also need to patch the array definition or is it just the interface?
    – Fenton
    Sep 14, 2015 at 9:54
  • On typescript 2.4.1 it needs added declare global {} on the interface declaration, otherwise it will trigger error
    – Pian0_M4n
    Jul 26, 2017 at 7:49
72

declare global seems to be the ticket as of TypeScript 2.1. Note that Array.prototype is of type any[], so if you want to have your function implementation checked for consistency, best to add a generic type parameter yourself.

declare global {
  interface Array<T> {
    remove(elem: T): Array<T>;
  }
}

if (!Array.prototype.remove) {
  Array.prototype.remove = function<T>(this: T[], elem: T): T[] {
    return this.filter(e => e !== elem);
  }
}
4
  • 1
    This does the trick, expect inside the function this will be any[], not T[]. Does anyone know why? Is there a workaround? Sep 28, 2018 at 4:59
  • 2
    I added an explicit type to the this parameter based on your question. See the "this parameters" section of typescriptlang.org/docs/handbook/functions.html. Sep 28, 2018 at 21:53
  • So how do you import this into another .ts file?
    – Sprotty
    Sep 25, 2020 at 14:09
  • @Sprotty use import './utils/extensions' e.g. the path to your file with the declaration Dec 6, 2022 at 22:30
20

Adding to Rikki Gibson's answer,

export {}

declare global {
    interface Array<T> {
        remove(elem: T): Array<T>;
    }
}

if (!Array.prototype.remove) {
  Array.prototype.remove = function<T>(elem: T): T[] {
      return this.filter(e => e !== elem);
  }
}

Without the export {}, you will get the TS error:

Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
3
  • 1
    How can we import this? Does "import * as array_extensions" work? or Is there any way to import just some of extension functions?
    – Saleh
    Sep 23, 2020 at 11:08
  • 4
    @Saleh Use import "./array_extensions"
    – grian
    Jan 1, 2021 at 8:54
  • I wonder why export {} helps?
    – tBlabs
    Jan 12, 2022 at 20:31
8

From TypeScript 1.6, you can "natively" extend arbitrary expressions like inbuilt types.

What's new in TypeScript:

TypeScript 1.6 adds support for classes extending arbitrary expression that computes a constructor function. This means that built-in types can now be extended in class declarations.

The extends clause of a class previously required a type reference to be specified. It now accepts an expression optionally followed by a type argument list. The type of the expression must be a constructor function type with at least one construct signature that has the same number of type parameters as the number of type arguments specified in the extends clause. The return type of the matching construct signature(s) is the base type from which the class instance type inherits. Effectively, this allows both real classes and "class-like" expressions to be specified in the extends clause.

// Extend built-in types

class MyArray extends Array<number> { }
class MyError extends Error { }

// Extend computed base class

class ThingA {
    getGreeting() { return "Hello from A"; }
}

class ThingB {
    getGreeting() { return "Hello from B"; }
}

interface Greeter {
    getGreeting(): string;
}

interface GreeterConstructor {
    new (): Greeter;
}

function getGreeterBase(): GreeterConstructor {
    return Math.random() >= 0.5 ? ThingA : ThingB;
}

class Test extends getGreeterBase() {
    sayHello() {
        console.log(this.getGreeting());
    }
}
2
7

Extending Array

Here is an example to extend Array and add the remove method to it. This is a JavaScript example

/** @template T */
class List extends Array {
  /**
   * Remove an item from the list and return the removed item
   * @param {T} item
   * @return {T}
   */
  remove(item) {
    const index = this.indexOf(item);
    if (index === -1) {
      throw new Error(`${item} not in list`);
    }
    this.splice(index, 1);
    return item;
  }
}

const arr = new List(1, 2, 3);
console.log(arr.remove(3)); // 3
console.log(arr); // [1, 2]

And this is a TypeScript example.

I've added a constructor and pushed the arguments of it to the array. (Couldn't do it with super(...items)!

class List<T> extends Array {
  constructor(...items: T[]) {
    super();
    this.push(...items);
  }

  public remove(item: T): T {
    const index = this.indexOf(item);
    if (index === -1) {
      throw new Error(`${item} not in list`);
    }
    this.splice(index, 1);
    return item;
  }
}
1
  • Just tried it with TypeScript and it worked very well and is very OOP idiomatic.
    – Kirill G.
    Aug 10, 2021 at 5:09
4
class MyArray<T> extends Array<T> {
    remove: (elem: T) => Array<T> = function(elem: T) {
        return this.filter(e => e !== elem);
    }
}
let myArr = new MyArray<string>();
myArr.remove("some");

this works for me with typescript v2.2.1!

2
  • 7
    This problem with this approach is that now every array has to be of the type MyArray, so it won't work seamlessly with other libraries that return arrays. Jul 18, 2018 at 20:59
  • 3
    The question was how to extend Array.prototype not just class inheritance Jul 30, 2020 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.