I have a number of private repositories that use the same Typescript types repeatedly so instead of copying them from one project to another, I have created a private NPM package that houses all of these shared types under a Typescript namespace. Each project installs that NPM package if it uses the shared types.
index.d.ts
export * as v1_6 from './types/version1_6';
export as namespace SharedNamespace;
One of the projects that use these types is a Serverless mono-repo that has this project structure:
package.json
tsconfig.json
|_ lib
|_ services
|_ service1
|_ service2
|_ service3
|_ service4
|_ serviceX
Where the tsconfig.json is in the parent directory and then there are several serverless projects in a services sub-directory, where only service1 has the private NPM types installed.
When I try to use the namespace inside of the service1 project, I get an error:
Cannot find namespace 'SharedNamespace'.
Which I assume I am getting for two reasons:
- Our types are not in the default
./node_modules/@typeslocation. - Since our NPM module is only Typescript namespaces we never
importit anywhere in our project
My question is, what is the best way to have it included in our global type scope?
My first attempt was to extend the base tsconfig.json file with a custom one inside of the service1 directory:
{
"compilerOptions": {
"typeRoots": [
"../../node_modules/@types",
"./node_modules/@types",
"./node_modules/@myOrg/privateTypes"
]
},
"extends": "../../tsconfig.json"
}
But even after the I get the same error.
However, if I do import the namespace manually where I need it, the error goes away. Ideally, we wouldn't have to import it all the time.
Update 1
If I use the following tsconfig.json:
{
"compilerOptions": {
"typeRoots": [
"./node_modules/@myOrg/privateTypes"
]
},
"extends": "../../tsconfig.json"
}
I now get the error Cannot find type definition file for 'types'. The file is in the program because: Entry point for implicit type library 'types'.
However in the package.json of the private NPM module:
"main": "",
"types": "index.d.ts",
"typeScriptVersion": "3.9.7",
and in the project directory:
So I am not sure why it cannot find that file?
