TypeScript declare module
In TypeScript 2.0+ there is a baseUrl
and paths
property in tsconfig.json
that you can use.
In your case, you would want:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"myapp/components": [
"menu/item",
"menu/logo",
"article/article",
"article/sidebar",
],
"myapp/templates": [
"templates/*" // whatever files you want to include
]
}
// etc...
},
// etc...
}
In baseUrl
you can specify the base directory (usually the root directory of the project). This allows you to do imports from anywhere in the directory structure as if you were doing an import from the directory specified in baseUrl
.
For example, with the directory structure...
folder1
- folder2
- file2.ts
file1.ts
...specifying a "baseUrl": "."
would allow you to import file1.ts
in file2.ts
as if you were in the root directory by doing:
import * as File1 from "file1";
On top of baseUrl
you can add paths
. This is useful for you and it must be used with a baseUrl
. In paths
you can specify patterns to match and a list of files to include in that pattern. This is illustrated in the github issue like so:
"paths": {
"pattern-1": ["list of substitutions"],
"pattern-2": ["list of substitutions"],
...
"pattern-N": ["list of substitutions"]
}
Note that this will just make it compile. I believe you'll also have to configure SystemJS in order to get this to work at runtime and that might require using a barrel file and setting the path to point to the barrel files.
You can read more about this in the github issue or read my other related answer here that also shows a pre TS 2.0 solution.
another way to use TypeScript declare module as below:-
create a file my-module.d.ts
declare module 'my-module' {
export default function anyName(arg1: string, arg2: string): MyResponse;
export interface anyName {
string: arg;
}
}
and then import as
import * as my-module from 'my-module';