【问题标题】:Can I get Ruby's method_missing behavior in typescript?我可以在打字稿中获得 Ruby 的 method_missing 行为吗?
【发布时间】:2023-12-29 13:37:01
【问题描述】:

如果一个类没有实现方法,我想处理引发的错误并使用方法名称注入我自己的代码。

例如:

class Foo {
    def method_missing(methodName) {
        console.log(`${methodName} was called`);
    }
}

let foo = new Foo();
foo.bar(); // should print 'bar was called' in the console

在 ruby​​ 中,每个提供此行为的类都有一个名为 method_missing 的方法。我们可以在打字稿中得到这个吗?如何得到?

【问题讨论】:

    标签: typescript method-missing


    【解决方案1】:

    不,TypeScript 不能这样做。

    【讨论】:

      最近更新 更多