如何在 child 中扩展 parent 的 属性

How to extend a parent's property in child

我有以下代码:

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

使用这段代码,我得到以下错误:

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

如何将 first getter 添加到 pagination?将该逻辑移动到 class 中是唯一的方法吗?

如果您不需要支持旧版浏览器,您可以将编译目标更改为 ES2018(或更高版本),这样传播语法就不会被转译。

Playground


关于 TestCafe,here 您可以找到如何自定义其运行器使用的 typescript 编译器选项(它不会选择标准的 tsconfig 文件)。