代码之家  ›  专栏  ›  技术社区  ›  Qwertie

检测函数是否返回了“void”(而不是“undefined”)。

  •  0
  • Qwertie  · 技术社区  · 7 年前

    void null|undefined

    var x: void = undefined, y: void = null;
    

    但我遇到了一个无法理解的问题。

    strictNullChecks "strictNullChecks": true 所以我想有什么东西暂时混淆了IDE,令人惊讶的是,新的错误可能是由不太严格的模式引起的。我应该在 模式, var y: void = null undefined

    能够 .具体来说,你可以回来 {stop:true}

    type ForRangeResult = {stop?:boolean} | undefined;
    class BTree<K=any,V=any> {
      forEach(callback: (k:K, v:V) => ForRangeResult): number { ... }
      ...
    }
    
    // Type 'void' is not assignable to type '{ stop?: boolean; }'.
    new BTree().forEach((k,v) => {});
    

    如果我使用

    type ForRangeResult = {stop?:boolean} | void;
    

    type ForRangeResult = {stop?:boolean} | undefined | null;
    

    {stop?:boolean} | void ,如何检查函数是否返回 control-flow-based typing 这里不工作:

    var result = callback(keys[i], values[i]);
    if (result !== undefined && result !== null) {
      // [ts] Property 'stop' does not exist on type 'void'
      if (result.stop)
        return ...;
    }
    

    TSC——版本

    1 回复  |  直到 7 年前
        1
  •  1
  •   cybersam    7 年前

    你的这种形式 if

    if (result instanceof Object) {
    
    推荐文章