以下代码来自 Deno Chalk 库的README。Deno/Typescript 不会让它通过:
import chalk from "https://deno.land/x/chalk_deno@v4.1.1-deno/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!");
输出:
错误:TS2339 [错误]:类型'{(...arguments_:any [])上不存在属性'blue':字符串;粉笔:粉笔类型;}'。console.log(chalk.blue("Hello world!"));
修补:
注释掉第 3 行,它运行良好!所以chalk.blue
在运行时可用但对编译器不可见?
函数在运行时没问题!