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?
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.