希望为控制台接口的方法使用变量。
而不是使用大的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'