打字稿中的文件系统

FileSystem in Typescript

如何在 Angular 4 应用程序的 TypeScript 中使用文件系统 API?

我将 @types/filesystem 添加到 devDependencies,但我仍然会遇到类似

的编译错误

Property 'resolveLocalFileSystemURL' does not exist on type 'Window'.

Cannot find name 'FileError'.

Cannot find name 'FileEntry'.

我的tsconfig.json和angular-cli生成的几乎一样,我只是在typeRoots中添加了"node_modules/typed-cordova-plugin-purchase:

{
  "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 的例子?

我最终通过将 types 添加到 tsconfig.json 使其工作,实际上是 tsconfig.app.json(我使用的是 angular CLI 1.0.1)

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

使用 @types/filesystem 是不够的,因为那里没有定义 FileError,这就是我使用 "@types/cordova-plugin-file.

的原因

作为对先前答案的补充,如果您还没有,请务必将@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-文件已安装!)