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

1

7

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?

Nonsmoker answered 24/9, 2020 at 8:15 Comment(2)
Is that a .d.ts file?Chacon
@AluanHaddad Yes, it is. Sorry, I forgot to mention that. Will add it to the question.Nonsmoker
C
12

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.

Chacon answered 24/9, 2020 at 8:26 Comment(4)
Thanks a lot for that information. I get this warning when I convert the file to ts: ES2015 module syntax is preferred over custom TypeScript modules and namespaces.. I will see if I can disable the "skipLibCheck": true. I activated it because @types/styled-components was triggering lots of type conflicts.Nonsmoker
It's hard to tell based on that error because I can't see the whole file. You should avoid writing declare module "module_specifier" {} in any context other than module augmentation because it pollutes the global declaration space. If you can point me at that file, I might be able to give you a pointer.Chacon
Here is my intent: I would like my project types to be accessible throughout all of my source files without having to import them. For example on test.ts, I'd like to do const foo: MY_PROJECT_TYPES.FOO and it works. The way I've found out how to do that is by using declare namespace on d.ts files. Considering the warning I got when I converted to .ts files, it seems that this is not recommended outside d.ts files. What is the best practice for this pattern? Thanks!Nonsmoker
It is not recommended inside .d.ts files either. Note that it isn't declare namespace that is to be avoided, but globals. It's a complex topic I can't cover in a comment, but what you see in various third party declarations is only global when the file isn't a module (no top-level import or export anywhere in it). I will say that I believe your intent is problematic and this has nothing to do with .d.ts vs .ts. You shouldn't pollute the global namespace, with types or values, and the conflicts that compelled you to set --skipLibCheck are evidence of why.Chacon

© 2022 - 2024 — McMap. All rights reserved.