我試圖使用fs.readFile
像這樣打字稿...fs.readFile(字符串,編碼) - TS2345
import {readFile} from 'fs';
let str = await readFile('my.file', 'utf8');
它導致這個錯誤:
TS2345: Argument of type '"utf8"' is not assignable to parameter of type '(err: ErrnoException, data: Buffer) => void'
我使用打字稿2.5.2,和@類型/節點8.0.30
我不知道你的typeScript錯誤,但是你不能執行'await readFile(...)',因爲'readFile()'不返回一個承諾。 「await」只適用於返回承諾的函數。 – jfriend00
@ jfriend00非常感謝您的支持,並沒有得到那麼多。我確實想知道,但無法找到異步等待的文檔。 – Drahcir
網上有數百篇關於在Javascript中使用異步和等待的文章。你需要閱讀以瞭解他們的工作方式。你可以從這裏開始:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await – jfriend00