7

I am creating an Angular 6 application (using TypeScript 2.7) and want to use the TextDecoder native function. When I run ng build --aot I receive the error: error TS2304: Cannot find name 'TextDecoder'.

Any help would be appreciated. Below is my tsconfig.json file and the list of packages from my package.json file.

Thanks, Bob

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

  "dependencies": {
    "@angular/animations": "^6.0.5",
    "@angular/cdk": "^6.2.1",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/material": "^6.2.1",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "bootstrap": "^4.1.1",
    "core-js": "^2.5.4",
    "hammerjs": "^2.0.8",
    "npm": "^6.1.0",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^10.5.7",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}
king_ozymandias
  • 123
  • 1
  • 3

4 Answers4

9

You need to change the setting of your output bundles into ES7. Go to src/tsconfig.app.json & add to compilerOptions:

"compilerOptions": {
    "lib": [
        "es2017",
        "dom"
    ]
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Melchia
  • 22,578
  • 22
  • 103
  • 117
3

The declaration of TextDecoder is present only in TypeScript 2.8 and newer. You'll need to either upgrade TypeScript or copy the relevant declarations from here into your project.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
3

write this on top of component.ts: For Angular 5 or early

declare var TextDecoder: any

Solve issue for me on typescript - 2.5.x

See post on typescript getting error TS2304: cannot find name ' require'

0

I found the solution here: https://stackoverflow.com/a/58490704/9091402

To use TextDecoder with TypeScript 2.7, define window as any

const textDecoder = new (window as any).TextDecoder('utf-8');