TypeScript中的文件系统

3
我该如何在Angular 4应用程序中使用TypeScript的FileSystem API?
我已经将@types/filesystem添加到devDependencies,但仍然会出现编译错误,例如:

属性“resolveLocalFileSystemURL”在类型“Window”上不存在。

找不到名称“FileError”。

找不到名称“FileEntry”。

我的tsconfig.json几乎与由angular-cli生成的相同,我只是将"node_modules/typed-cordova-plugin-purchase"添加到了typeRoots中。
{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types",
            "node_modules/typed-cordova-plugin-purchase"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

我需要导入什么吗? 有使用TypeScript中FileSystem的示例吗?


请展示你尝试使用它的代码。 - Fredrik Lundin
1
请参阅 https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types。确保您没有`types`或者如果您有它们,请将其添加到`types`中。适当的`typeRoots`从来不会有坏处。请在问题中提供tsconfig。 - Estus Flask
@estus,我更新了我的问题。 - Adrian Ber
1
@FredrikLundin,即使执行window.requestFileSystem也会抛出相同类型的错误。 - Adrian Ber
2个回答

6

我最终通过在tsconfig.json中添加types(实际上是tsconfig.app.json,因为我正在使用angular CLI 1.0.1)来使其工作。

"types": [
    "cordova",
    "cordova-plugin-file"
]

仅使用@types/filesystem是不够的,因为它没有定义FileError,所以我使用了"@types/cordova-plugin-file


1
但是为什么要“借用”cordova类型定义呢?这些应该作为标准DOM例程的一部分进行定义,对吧? - user9315861
显然,它似乎没有包含在标准DOM中。据我所知,这个API仍然是实验性的,并且建议在它更加稳定之前不要为其实现类型。您可以在此处查看讨论:https://github.com/microsoft/TypeScript/issues/29548。cordova-plugin-file似乎是一个很好的替代品。我会尝试一下。 - Lunfel

2
作为对之前答案的补充,如果您还没有这样做,请务必将@types/cordova(可能还有@types/cordova-plugin-file...请参见下面的说明)添加到您的项目中。这是我的情况:将基于Angular 5的应用程序编译成另一个基于Cordova的目标时需要这样做。
npm install @types/cordova --save
npm install @types/cordova-plugin-file --save

后面的插件声明:这是Apache Cordova文件系统插件的存根类型定义。Apache Cordova文件系统插件提供自己的类型定义,因此您不需要安装@types/cordova-plugin-file!

最初的回答:

另一个插件声明:这只是Apache Cordova文件系统插件的类型定义,实际上它本身就提供了类型定义,所以你不需要安装@types/cordova-plugin-file!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接