代码之家  ›  专栏  ›  技术社区  ›  Balu Vyamajala

使用存储在变量中的类型打印不同类型的控制台消息(console.debug、console.info等)

  •  0
  • Balu Vyamajala  · 技术社区  · 5 年前

    希望为控制台接口的方法使用变量。

    而不是使用大的if条件

    if (method == 'debug')
     console.debug(val)
    
    if (method == 'error')
     console.error(val)
    
    if (method == 'info')
     console.info(val)
    

    我用Javascript做了以下工作,效果很好:

    const method='debug';
    const val = 'to be printed';
    console[method](val);
    

    我怎样才能在打字脚本中实现同样的功能?javascript中的同一行返回以下错误:

    Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Console'.
      No index signature with a parameter of type 'string' was found on type 'Console'
    
    0 回复  |  直到 5 年前
    推荐文章