2

How do i import FileReader in typescript. I tried this:

var reader:FileReader = new FileReader();

my tsconfig.json

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "out",
    "lib": [
        "es6"
    ],
    "sourceMap": true,
    "rootDir": "."
},
"exclude": [
    "node_modules",
    ".vscode-test"
]
}

My error is cannot find name 'FileReader' I am unable to do this too:

import FileReader;
5
  • what file reader are you talking about? Commented Feb 2, 2017 at 15:41
  • you have to provide more info: your tsconfig.json, where is FileReader defined, it is a typescript or javascript class.... Commented Feb 2, 2017 at 15:55
  • 2
    Standard FileReader (as present in JavaScript without libraries) is in lib.d.ts and you don't need to import anything. Check how old your lib.d.ts is. I think FileReader wasn't in lib.d.ts historicaly because lib.d.ts is exported from Internet Explorer and FileReader wasn't in older IE. Commented Feb 2, 2017 at 20:18
  • I tried using FileReader without import but im getting error cannot find name 'FileReader' Commented Feb 3, 2017 at 5:25
  • The FileReader API is defined in lib.dom.d.ts as seen here: github.com/Microsoft/TypeScript/blob/master/lib/… it should work without doing anything. If not, you can try to locate that file on your disk and use a triple-slash directive to force TypeScript to load it : typescriptlang.org/docs/handbook/triple-slash-directives.html but that would denote a flaw in your setup. Commented Jun 23, 2017 at 8:07

1 Answer 1

4

I ran into this issue as I forgot to add "dom" to the "lib" node in the tsconfig.json.

Fix:

In your tsconfig.json ensure under "lib" you have "dom". Here is my current tsconfig.json

{
   "compilerOptions": {
      "lib": [
         "dom",
         "dom.iterable",
         "esnext",
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

See the below ugly themed VS code the lib.dom.d.ts is where the FileReader interface lives which made me realise I was missing "dom"

enter image description here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.