代码之家  ›  专栏  ›  技术社区  ›  Ben Winding

你能声明语言特征吗?例如:`!变量`as`无变量`

  •  0
  • Ben Winding  · 技术社区  · 7 年前

    是否可以在应用程序的源代码中声明Typescript语言特性?

    if (!userDetails) {
       // handle null
    }
    

    新的空测试

    if (no userDetails) {
       // handle null
    }
    

    只是在我看来稍微好一点。。。如果能帮我找到正确的方向,那就太好了,谢谢!

    :在handle中处理null当没有设置对象时,我知道这也会处理 false '' undefined 等。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Code Maniac    7 年前

    你可以用这个模板做一些类似的事情

    function no(value){
      return arguments[1] ? true : false
    }
    
    let userDetails = null
    
    if(no`${userDetails}`){
      console.log('not null')
    } else {
      console.log('null')
    }
    
    if(no`${4}`){
      console.log('not null')
    }
        2
  •  0
  •   basarat    7 年前

    no userDetails

    不支持。不支持这样的语法添加。

    更多

    从技术上讲,你可以使用语言插件来添加它们,但这项工作很辛苦,而且有点晦涩难懂。

        3
  •  0
  •   Andre Pena    7 年前

    请记住,TypeScript的语义非常好,以确保它不会与JavaScript语法冲突。对于你提出的这个例子,我想不出会发生碰撞,但还是有可能的。