![](/img/trans.png)
[英]How do I add custom property to Express.Request in Typescript?
[英]How do I add Blob to Typescript?
我正在嘗試使用 FileSaver 保存文件,因此需要 Blob 格式。 我的應用程序是用打字稿編寫的。
當我嘗試做
import * as Blob from "blob";
我收到錯誤
Could not find a declaration file for module 'blob'. '/Users/path/to/project/node_modules/blob/index.js' implicitly has an 'any' type.
Try \npm install @types/blob` if it exists or add a new declaration (.d.ts) file containing `declare module 'blob';``
我不能只導入“blob”,因為@types/blob
包不存在。
我用這些內容創建了一個聲明文件blob.d.ts
declare module 'blob' {
function Blob(blobParts[, options]): any
export = Blob
}
這並沒有解決我需要 Blob 的一行代碼的問題
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
它返回了下面的錯誤
Argument of type '{ type: string; }' is not assignable to parameter of type '[any, any]'. Object literal may only specify known properties, and 'type' does not exist in type '[any, any]'.
這個類似的問題不是重復的,它的答案對我沒有幫助,因為該聲明文件不聲明模塊並且不導出任何內容。
在您的tsconfig.json
,確保在您的lib
數組中包含webworker
:
{
"compilerOptions": {
...
"lib": ["webworker"],
...
},
...
}
盡管從上面提供的參考鏈接應該已經很明顯, lib.webworker.d.ts
包含Blob
和相關接口所需的聲明。
interface Blob {
readonly size: number;
readonly type: string;
slice(start?: number, end?: number, contentType?: string): Blob;
}
declare var Blob: {
prototype: Blob;
new(blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.