Provide the compiler with information about the global type definition that we set via npm @types

Now we can install declaration files like this.

npm install --save @types/whatwg-fetch

Types are set here.

node_modules
  @types
    whatwg-fetch
      index.d.ts
      package.json
      README.md
      types-metadata.json
    whatwg-streams
      index.d.ts 
      package.json
      README.md
      types-metadata.json

Among other things, whatwg-fetch / README.md talks about this.

 * File structure: Global
 * Global values: Headers, Request, Response, fetch

And the TypeScipt Documentation says this :

By default, all visible @types packages are included in your compilation. Packages in the node_modules / @ types of any closing folder are considered visible ...

Despite this, the following errors occur when compiling my project with TypeScript 2.0:

aurelia-fetch-client.d.ts(17,23): error TS2304: Cannot find name 'Request'.
aurelia-fetch-client.d.ts(17,45): error TS2304: Cannot find name 'Response'.
aurelia-fetch-client.d.ts(68,13): error TS2304: Cannot find name 'Headers'.

What do we need to do so that the compiler knows about these types?

As a workaround, we added the following to our tsconfig.json file. However, from TypeScript documentation this is not necessary.

"compilerOptions": {
    "types": [ "whatwg-fetch" ],
+4

Source: https://habr.com/ru/post/1657006/


All Articles