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

Javascript 使用存储在变量中的类型打印不同类型的控制台消息(console.debug、console.info等),javascript,node.js,typescript,Javascript,Node.js,Typescript,希望为控制台接口的方法使用变量 而不是使用大的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]

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

而不是使用大的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);
如何在Typescript中执行相同的操作?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'

除非您使用的是
let
而不是
const
方法
应键入文本
“debug”
,而不是
字符串
。此代码应该可以正常工作:
let方法:“debug”|“error”|“info”