3

How can I use FileSystem API in TypeScript in an Angular 4 application?

I added @types/filesystem to devDependencies, but I still get compile errors like

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

Cannot find name 'FileError'.

Cannot find name 'FileEntry'.

My tsconfig.json is almost the same as the one generated by angular-cli, I just added "node_modules/typed-cordova-plugin-purchase to 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"
    ]
  }
}

Should I import anything? Are there any examples of using FileSystem in Typescript?

4
  • Show us the code where you try to use it Commented Apr 29, 2017 at 11:34
  • 1
    See typescriptlang.org/docs/handbook/… . Make sure you don't have types or you added it to types if you have them. Proper typeRoots never hurts. Please, provide tsconfig in the question. Commented Apr 29, 2017 at 11:45
  • @estus, I updated my question. Commented Apr 29, 2017 at 20:20
  • 1
    @FredrikLundin, even doing window.requestFileSystem throws the same kind of error. Commented Apr 29, 2017 at 20:22

2 Answers 2

6

I made it finally work by adding types to tsconfig.json, actually tsconfig.app.json (I'm using angular CLI 1.0.1)

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

Using @types/filesystem is not enough as FileError is not defined there, that's why I used "@types/cordova-plugin-file.

Sign up to request clarification or add additional context in comments.

2 Comments

But why would one have to "borrow" the cordova type definitions? These should be defined somewhere as part of the standard DOM routines, right?
Apparently it seems like its not included in standard DOM. As I understand it, this api would still be experimental and it has been recommended not to implement types for it until it is more stable. You can see discussion here: github.com/microsoft/TypeScript/issues/29548. Seems like the cordova-plugin-file could be a good alternative. I will try it out.
2

As an addition to the previous answer, be sure to add @types/cordova (and possibly @types/cordova-plugin-file... see note below) to your project if you have not done so already. That was my situation compiling an angular 5 based app to an alternate cordova-based target.

npm install @types/cordova --save
npm install @types/cordova-plugin-file --save

(the latter plugin states: This is a stub types definition for Apache Cordova File System plugin. Apache Cordova File System plugin provides its own type definitions, so you don't need @types/cordova-plugin-file installed!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.