使用闭包编译器创建结果类型

Creating a Result type with Closure Compiler

我正在使用 Closure Compiler,我想要一个类似 Rust 的 Result 类型,它要么包含特定类型的值,要么不包含任何值和错误,以表示 return 来自函数的值。

这是我的意思的一个例子:

/**
 * @param {function(Result<Element>)} callback
*/
function bar (callback) {        
    if (...) {
        var elem = ...;
        callback(Result.ok(elem));
    } else {
        callback(Result.err(new Error("...")));
    }
}

bar(function (result) {
    if (result.ok) {
        // guaranteed to be a non-error result here - no warning
        console.log(result.val.nodeType);
    } else {
        // closure should produce a warning here about undefined prop
        console.log(result.val.nodeType);
    }
});

可能的实现(虽然不会抛出警告):

/**
 * @constructor
 * @template T
 * @param {boolean} ok
 * @param {T} val
 * @param {Error} err
**/
function Result (ok, val, err) {
    this.ok = ok;
    this.val = val;
    this.err = err;
}

/**
 * @template T
 * @param {T=} val
 * @return {OkResult<T>}
**/
Result.ok = function (val) {
    return new OkResult(val);
};

/**
 * @param {Error} err
 * @param {Error=} previous
 * @return {ErrResult}
**/
Result.err = function (err, previous) {
    err['previous'] = previous;
    return new ErrResult(err);
};

/**
 * @constructor
 * @extends {Result}
 * @template T
 * @param {T} val
**/
function OkResult (val) {
    this.ok = true;  
    this.val = val;
    this.err = null;
}

/**
 * @constructor
 * @extends {Result}
 * @param {Error} err
**/
function ErrResult (err) {
    this.ok = false;
    this.val = null;
    this.err = err;
}

我试图用一个 Result 超类和两个 OkResultErrResult 子类来实现它,但是当我尝试编写应该产生警告的代码时,我没有得到任何。

是否有某种方法可以创建具有上述指定属性的 Result 类型?一种在尝试访问错误结果时会安全警告的方法,就好像它是一个正常结果一样?

使用 classical 继承绝对是做到这一点的方法。而不是测试 obj.ok 属性,检查应该是 instanceof.

bar(function (result) {
  if (result instanceof OkResult) {
    // instanceof tests are recognized by the compiler
    // and automatically tightens the types.
    console.log(result.val.nodeType);
  } else if (result instanceof ErrorResult) {
    // closure produces a warning here about undefined prop
    console.log(result.val.nodeType);
  }
});

此外,编译器仅在 属性 不存在时才发出缺少属性的警告。为了实现这一点,属性 不得在任何父 class.

上定义

See a working example