Search code examples
typescripttypescript-typingstsconfig

Typescript is not detecting errors for nonexistent type names in declaration "d.ts" files


In this example in Typescript playground I'm trying to use an nonexistent type inside a namespace and I get an error:

enter image description here

enter image description here

This is expected.

But in my local dev environment, Typescript is basically accepting any nonexistent type as any.

Note: This is only happening inside d.ts files.

enter image description here

enter image description here

Don't know if it matters, but I'm using the noImplicitAny: true flag.

See my tsconfig.json file:

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "jsx": "react",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "target": "ES6",
    "paths": {
      "@src/*": ["./src/*"],
    }
  },
  "include": [
    "src/**/*",
    "functions/src/**/*",
    "functions/index.ts"
  ],
}

How can I make Typescript detect those errors?


Solution

  • Declaration files, files with a .d.ts extension, are covered by the --skipLibCheck compiler option.

    By specifying

    {
      "compilerOptions": {
        "skipLibCheck": true
      }
    }
    

    in your tsconfig.json, you have told TypeScript not to validate this file.

    Broadly speaking, you have two options to enforce validation of this file.

    1. Rename the file from .d.ts to .ts. This is the most direct approach, and the least invasive. .ts files that contains only declarations are perfectly valid.

    2. Remove the "skipLibCheck": true configuration above thereby enforcing type checking in .d.ts files which is the default behavior.