typescript - Chalk 在 Deno 中无法运行

标签 typescript deno chalk

以下代码来自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/

相关文章:

javascript - 我可以将基于 Node 的项目迁移到 Deno 吗?

javascript - 在 chalk.js 中的 forEach() 中获取随机颜色

node.js - 使用 chalk NPM 模块的终端样式(控制字符)

typescript - 如何为 chalk 的标记模板文字定义 TypeScript 类型?

typescript - 将不正确的变量类型传递给函数不会发出类型错误

typescript - inlineSourceMap 和 inlineSources typescript 编译器选项之间的区别

javascript - 以特定方式在JS中导入

compiler-errors - TS2345 [ERROR] : Argument of type '{ depth: and Property ' response' does not exist on type '{ respnse: any; }' on Deno compile

deno - 我可以允许已编译的 Deno 可执行文件访问运行时提供的单个主机名吗?

javascript - JS谷歌地图修改另一个类的监听器