Home > Software engineering >  Why do I have to specify types in tsconfig.json if type definitions are already in package.json
Why do I have to specify types in tsconfig.json if type definitions are already in package.json

Time:11-15

I have "@types/gapi.client.gmail": "~1.0.3" in package.json.

I also had this in tsconfig.json:

  "types": [
    "jest",
    "gapi",
    "gapi.auth2",
    "gapi.client.docs"
  ],

I can't recall why those statements are there. But now that I am adding gmail support to my app, I want to understand why I need to add gmail to this type array, so it becomes:

  "types": [
    "jest",
    "gapi",
    "gapi.auth2",
    "gapi.client.docs",
    "gapi.client.gmail"
  ],

Without this addition, I get:

TS2339: Property 'gmail' does not exist on type 'typeof client'.

Why do I have to add types to the tsconfig array if they types are already declared in package.json as dependencies?

CodePudding user response:

When you define your types like that, only the types in the list will be imported, and not all of the available types under your node_modules.

Relevant docs- https://www.typescriptlang.org/tsconfig#types

  • Related