6 标准库

2021-05-31  本文已影响0人  9e8aeff1c70c

Deno提供了一组标准模块,这些模块由核心团队审核,并保证可以与Deno一起使用。

标准库可从以下网址获得:https://deno.land/std/

版本化和稳定性

标准库还不稳定,因此它的版本不同于Deno。有关最新版本的信息,请咨询https://deno.land/std/https://deno.land/std/version.ts.或。每次发布Deno时,都会发布标准库。

我们强烈建议始终使用固定标准库版本的导入,以避免意外更改。例如,不是链接到默认的代码分支(它可能随时更改,可能会导致编译错误或意外行为):

// import the latest release, this should be avoided
import { copy } from "https://deno.land/std/fs/copy.ts";

取而代之的是使用了一个不可变且不会更改的STD库版本:

// imports from v0.95.0 of std, never changes
import { copy } from "https://deno.land/std@0.95.0/fs/copy.ts";

找错

标准库中提供的一些模块使用不稳定的Deno API。

尝试在没有--unstableCLI标志的情况下运行这类模块时,会出现大量的打字错误,这表明当前Deno命名空间中的一些API不存在:

// main.ts
import { copy } from "https://deno.land/std@0.95.0/fs/copy.ts";
copy("log.txt", "log-old.txt");
$ deno run --allow-read --allow-write main.ts
Compile file:///dev/deno/main.ts
Download https://deno.land/std@0.95.0/fs/copy.ts
Download https://deno.land/std@0.95.0/fs/ensure_dir.ts
Download https://deno.land/std@0.95.0/fs/_util.ts
error: TS2339 [ERROR]: Property 'utime' does not exist on type 'typeof Deno'. 'Deno.utime' is an unstable API. Did you forget to run with the '--unstable' flag?
    await Deno.utime(dest, statInfo.atime, statInfo.mtime);
               ~~~~~
    at https://deno.land/std@0.95.0/fs/copy.ts:92:16

TS2339 [ERROR]: Property 'utimeSync' does not exist on type 'typeof Deno'. 'Deno.utimeSync' is an unstable API. Did you forget to run with the '--unstable' flag?
    Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
         ~~~~~~~~~
    at https://deno.land/std@0.95.0/fs/copy.ts:103:10

要解决这个问题,需要添加--unstable属性标志:

deno run --allow-read --allow-write --unstable main.ts

为了确保接口产生错误是不稳定的,请检查lib.deno.unstable.d.ts的声明。

这个问题应该在不久的将来得到解决。如果您所依赖的特定模块在没有该标志的情况下成功编译,可以随意省略该标志。

上一篇 下一篇

猜你喜欢

热点阅读