Deno 標(biāo)準(zhǔn)庫(kù)

2020-06-24 17:06 更新

Deno 提供一組標(biāo)準(zhǔn)模塊,它們經(jīng)過(guò)核心團(tuán)隊(duì)審計(jì),保證能在 Deno 上工作。 標(biāo)準(zhǔn)庫(kù)地址:https://deno.land/std/

版本和穩(wěn)定性

標(biāo)準(zhǔn)庫(kù)尚不穩(wěn)定,因此采用與 Deno 不同的版本號(hào)。每次 Deno 發(fā)布時(shí),標(biāo)準(zhǔn)庫(kù)也會(huì)一起發(fā)布。 最新的發(fā)布請(qǐng)查閱 https://deno.land/std/https://deno.land/std/version.ts。 我們強(qiáng)烈建議:始終使用確定版本的標(biāo)準(zhǔn)庫(kù),以避免意外的改動(dòng)。 例如,連接到隨時(shí)可能更改的主分支時(shí)可能會(huì)導(dǎo)致編譯錯(cuò)誤或意外行為:

// 從 master 導(dǎo)入,這應(yīng)當(dāng)避免
import { copy } from "https://deno.land/std/fs/copy.ts";

更好的選擇是使用不可變且不會(huì)更改的 std 庫(kù)版本:

// 從不可變的 std v0.50.0 導(dǎo)入
import { copy } from "https://deno.land/std@0.50.0/fs/copy.ts";

排錯(cuò) (Troubleshooting)

標(biāo)準(zhǔn)庫(kù)中的一些模塊使用了不穩(wěn)定的 Deno API。 不用 --unstable 命令行選項(xiàng)運(yùn)行這些模塊會(huì)產(chǎn)生一些 TypeScript 錯(cuò)誤,表示 Deno 命名空間中不存在一些 API:

// main.ts
import { copy } from "https://deno.land/std@0.50.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.50.0/fs/copy.ts
Download https://deno.land/std@0.50.0/fs/ensure_dir.ts
Download https://deno.land/std@0.50.0/fs/_util.ts
error: TS2339 [ERROR]: Property 'utime' does not exist on type 'typeof Deno'.
    await Deno.utime(dest, statInfo.atime, statInfo.mtime);
               ~~~~~
    at https://deno.land/std@0.50.0/fs/copy.ts:90:16


TS2339 [ERROR]: Property 'utimeSync' does not exist on type 'typeof Deno'.
    Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
         ~~~~~~~~~
    at https://deno.land/std@0.50.0/fs/copy.ts:101:10

解決方法是加上--unstable 選項(xiàng):

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

要確定哪些 API 是不穩(wěn)定的,請(qǐng)查閱類型聲明 lib.deno.unstable.d.ts

這個(gè)問(wèn)題會(huì)在不遠(yuǎn)的將來(lái)解決。如果您依賴的特定模塊在沒(méi)有該選項(xiàng)的情況下成功編譯,則可以忽略該選項(xiàng)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)