Based on the information provided by the TypeScript website, TypeScript has the capability to automatically load types that are located in folders as long as they have been referenced in your code.
@types, typeRoots and types
By default, all visible "@types" packages are included during compilation. Any packages found in node_modules/@types within any parent folder are considered visible, including those in ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so forth.
If a specific typeRoots location is specified, only packages under that location will be included. For example:
{
"compilerOptions": {
"typeRoots" : ["./typings"]
}
}
This configuration file will incorporate all packages under ./typings while excluding packages from ./node_modules/@types
You can easily test this setup by following these steps:
tc --init
Create an index.d.ts
file inside @types/index.d.ts
, and enter the following code:
declare interface Foo {
Bar: string;
}
In the main folder, create a new index.ts
file and test it using your code editor (e.g., VSCode):
let foo:Foo;
foo. // you will see code completion suggestions
Additional note:
Whether your code resides inside @types or not, TypeScript will automatically locate them. You also have the option to manually specify the path for typeRoots but remember to configure it to include @types within node_modules
.