0

I expect that a class return an array when I new it

class MyArray {
  constructor(){

  }
}

const myArray = new MyArray()
Array.isArray(myArray) // Should be true

I used to write it in this way:

class MyArray {
  constructor(){
    const arry = new Array()
    return arry
  }
}

But when I write in Typescript,the return value arry is not the type of MyArray, so it prompt an error.

How to fixed this problem?

SWAN ZHANG
  • 31
  • 2

1 Answers1

7

Just extend Array and return true in the constructor

class MyArray extends Array{
  constructor(){
    super()
  }
}

Demo

class MyArray extends Array{
  constructor(){
    super()
  }
}

myArray2 = new MyArray()
console.log(Array.isArray(myArray2));
gurvinder372
  • 66,980
  • 10
  • 72
  • 94