chalk 包对于Node 中的终端样式非常流行。
我正在使用打字稿。我正在尝试动态设置颜色。但是我在编译时遇到了 TS 错误。
代码:
import chalk, { Chalk } from 'chalk';
function getColor(): keyof Chalk {
let color: keyof Chalk = 'green';
color = 'yellow';
return color;
}
const chalkColor = getColor();
// Error on `chalk[chalkColor]`
console.log(chalk[chalkColor]('message'));
错误(开chalk[chalkColor]
):
This expression is not callable.
Not all constituents of type 'boolean | (Chalk & { supportsColor: ColorSupport; }) | (ChalkConstructor & Function) | Level | ((r: number, g: number, b: number) => Chalk & { supportsColor: ColorSupport; }) | ... 10 more ... | ((color: string) => Chalk & { ...; })' are callable.
Type 'false' has no call signatures.
如何解决此错误?