以下代码来自README Deno Chalk 图书馆的。 Deno/Typescript 不会让它通过:
import chalk from "https://deno.land/x/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dad1d8d5d2e6dddcd7d6f9cf8d9788978894dddcd7d6" rel="noreferrer noopener nofollow">[email protected]</a>/source/index.js";
// Run this in debugger and it's fine but it won't compile:
console.log(chalk.blue("Hello world!"));
console.log(eval("typeof chalk.blue"), "At runtime it's fine!");
输出:
error: TS2339 [ERROR]: Property 'blue' does not exist on type '{ (...arguments_: any[]): string; Chalk: typeof Chalk; }'. console.log(chalk.blue("Hello world!"));
修补:
注释掉第 3 行,它运行得很好!所以 chalk.blue
在运行时可用,但对编译器不可见??
function At runtime it's fine!
最佳答案
我们知道 chalk 是 Node 原生的(因为它主要是为 Nodejs 构建的),并且它在 deno 上的移植会存在一些错误和缺点,因为它们不像它们的父包那样成熟,更不用说 deno 本身是相当新的并且需要时间来建立一个强大的开发人员友好的环境。
但在您的情况下,chalk
模块可以轻松地替换为名为 colors
的 deno 内置模块。
链接:-https://deno.land/[email protected]/fmt/colors.ts
The link is in the format of
https://deno.land/std@version/fmt/colors.ts
where version should be replaced by version you want to use, at the time of writing the latest version is 0.123.0
用法:
// importing colors library from denoland
import {red,blue,bold} from "https://deno.land/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f6f1e1c5b5abb4b7b6abb5" rel="noreferrer noopener nofollow">[email protected]</a>/fmt/colors.ts"
console.log(red("This text will be printed in red");
console.log(blue("This text will be printed in blue");
// merging two properties
console.log(bold(red("This text will be red and bold"));
如您所见,其用法与 chalk 模块非常相似,对于完整的 API 文档,您可以查看提供的 README here在德诺兰
关于typescript - Chalk 在 Deno 中无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69538787/