Home > Software engineering >  How to extend a parent's property in child
How to extend a parent's property in child

Time:10-18

I have the following code:

class Table {
    get pagination () {
        return {
            get item () {
                return {
                    log (s : string) {
                        console.log(s);
                    }
                }
            }
        }
    }
}
class TableWithFirst extends Table {
    get pagination () {
        return {
            ...super.pagination,
            get first () {
                this.item.log("first");
                return "first";
            }
        }
    }
}
const t = new TableWithFirst();
t.pagination.first

and with this code, I get the error below:

[ERR]: "Executed JavaScript Failed:" 
[ERR]: Cannot read properties of undefined (reading 'log') 

How do I add first getter to pagination? Is moving that logic into a class the only way?

CodePudding user response:

If you don't need to support older browsers you can change compilation target to ES2018 (or higher) so spread syntax won't be transpiled.

Playground


Regarding TestCafe, here you can find how to customize typescript compiler options used by its runner (it won't pick up standard tsconfig file).

  • Related