#StackBounty: #typescript #typescript-generics difference between number and generic parameter

Bounty: 50

I am writing a class in typescript and don’t know how to differentiate between generic and number type parameter. Code example

class Test<T> {
  remove(value: T): boolean;
  remove(index: number): boolean;
  remove(indexOrValue: T|number): boolean {
    if (typeof indexOrValue === "number") {
      /* What about new Test<number>() */
      const index = indexOrValue as number;
      this.items.splice(index, 1)
      return true;
    } else {
      const index = this.items.indexOf(indexOrValue as T)
      if (index > -1) this.items.splice(index, 1);
      return true;
    }
    return false;
  }
}

PS: I don’t know this problem specifically that y i am writing here instead of searching


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.