1

I am using fetch in my angular project and the code snippet is as follows:

        fetch(
            'www.example.com', {
            headers: new Headers({
            'Origin': location.origin
            }),
            mode: 'cors'
        })

However, the website runs without problem but it gives error as below: error TS2304: Cannot find name 'fetch'. error TS2304: Cannot find name 'Headers'.

I searched the ways to solve this problem and tried add "lib": [ "es5", "dom"] in "compilerOptions" in tsconfig.json as below, but got the same error. I also tried adding "es6" inside, but no luck. I am running out of idea and could you please help.

"compilerOptions": {
        "lib": [ "es5", "dom"],
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "inlineSources": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noImplicitAny": false,
        "allowUnreachableCode": true
    },
bunny
  • 1,797
  • 8
  • 29
  • 58
  • Try `es2018` instead of `es5`. I'm not sure why `es6` didn't work, but `fetch` definitely isn't in `es5`. – SimpleJ Apr 11 '19 at 21:15
  • Should I change the target as well or just in the lib? – bunny Apr 11 '19 at 21:30
  • Probably. Honestly after reading [this](https://stackoverflow.com/questions/42093758/need-clarification-of-the-target-and-lib-compiler-options) answer about the difference I'm still not sure what the right answer is. – SimpleJ Apr 11 '19 at 21:35

0 Answers0