When it comes to including your library package, there are some common rules that you should keep in mind.
If you choose to import
the package as a module, the index.d.ts
file located at the root of the package (or specified in the package.json
entry) will be automatically detected using TypeScript's module resolution.
In case the package types consist solely of global type declarations and do not require importing from a module, TypeScript will search for them in the node_modules/@types
directory within any parent folder according to the typeRoots configuration.
For scenarios where you have a file with just declare var foo: number;
(meaning it is a global declaration file) and the types are located under node_modules/my-lib
rather than node_modules/@types/my-lib
, you will need to manually specify their inclusion in the compilation process by adding them to the files
property in your tsconfig.json
. Check out the documentation's example section here for further guidance.
An alternative approach would involve adjusting the typeRoots
setting, although this may be unnecessary complexity for your situation.
Another potential issue might stem from the use of npm link
, which creates symbolic links that TypeScript defaults to resolve during compilation. To address this, you can enable the --preserveSymlinks
option via tsc --preserveSymlinks
. This flag instructs TypeScript to consider the location of the symbolic link file instead of the resolved path when processing module references. For more information on this option, refer to the compiler options documentation.
According to the documentation, in this mode, module and package references such as imports and /// directives are resolved relative to the symbolic link file's location rather than the actual path of the symlink destination.