Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code recognizes custom.d.ts only when it's open

I have some custom Typescript declarations in a custom.d.ts file, and VS code will read my TS correctly when that file is open, however if the file is closed, VSCode does not "know" about those TS definitions, and I get red underlines all over my code.

One catch is that my tsconfig.json file is NOT in the root directory, and neither is my custom.d.ts file. Why? Because config files in a root directory is so stupid, and I hate it. Why can't config files like that go in a config directory???

Anyway, how can I instruct VSCode to KNOW about my custom.d.ts file? Again, if the file is open, VSCode knows about those definitions, but when I close the file, VSCode forgets about those definitions and the rest of typescript gets red error underlines...

Directory Structure:

/
| - package.json
| - .gitignore
| - client/
    | - client source code.... 
| - server/
    | - @types/
        | - custom.d.ts 
    | - configs/ 
        | - tsconfig.json
        | - .develop.env
like image 291
TJBlackman Avatar asked Jan 20 '26 02:01

TJBlackman


1 Answers

Simply put, you can add a directive above your entry file (suggested)

/// <reference types="./path/to/your.d.ts" />

which implies ambient type definitions can be found in that path.

And also, you can add that "d.ts" file to your tsconfig.json in "include" section like:

{
  "include": ["src", "mydef.d.ts"]
}

And further, you can add one loc: export {} to denote the declaration file to be modular (this file should be included from your tsconfig.json).

For further information, you can head towards typescriptlang.org

like image 89
Ryuujo Avatar answered Jan 22 '26 14:01

Ryuujo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!