javascripttypescriptvisual-studio-codetsconfigtsconfig.json

types.d.ts is not being automatically included in every TypeScript file


I've noticed that the types.d.ts file in the root of the project was automatically included in every TypeScript file in my project. I didn't have to write import type {...} from "./types.d.ts"; in every file manually, which is a very convenient feature.

But recently, that stopped being the case. VSCode shows errors when not writing the line above. What did I do? Could it be something with my tsconfig.json? More importantly, how do I get it back?

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "Node",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "paths": {
            "*": [
                "./node_modules/*"
            ]
        },
        "types": ["./types.d.ts"],
        "rewriteRelativeImportExtensions": true
    }
}

I manually included .ts extensions in import modules because at first I used tsc, which outputted the built files in the same place as the normal files, but that's beyond the scope of my question.
Also, I have not tried compiling without the ./types.d.ts import. The problem I have as of now is with VSCode.

Extra context: I can't provide types.d.ts because of privacy reasons, but I do have one import in it:

import { CacheType, ChatInputCommandInteraction } from "discord.js/typings";

Solution

  • Change your import to:

    type CacheType = import("discord.js/typings").CacheType;
    
    type ChatInputCommandInteraction = import("discord.js/typings").ChatInputCommandInteraction;
    

    I believe that if you use an import statement, the declaration is no longer treated as an ambient module, and therefore isn't available everywhere.