我正在尝试使用 FileSaver 保存文件,这必然需要 Blob 格式。我的应用程序是用 typescript 编写的。
当我尝试做的时候
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
}
基于MDN entry on the Blob constructor
这并没有解决我需要 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]'.
这similar question不是重复的,它的答案对我没有帮助,因为该声明文件没有声明模块,也没有导出任何东西。
最佳答案
在你的tsconfig.json
, 请确保包含 webworker
在你的lib
数组:
{
"compilerOptions": {
...
"lib": ["webworker"],
...
},
...
}
尽管从上面提供的引用链接中应该已经很明显,lib.webworker.d.ts
包含 Blob
和相关接口(interface)所需的声明。
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;
};
关于node.js - 如何将 Blob 添加到 Typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862973/