I am trying to write a typescript library that is shared between a few other typescript applications. To do this I put each library as its own node module, and have the library set to emit its declaration file.
To test I have the shared library setup with one file:
lib\guid.ts
export class Guid {
static EmptyGuid(): string {
return '00000000-0000-0000-0000-000000000000';
}
}
lib\tsconfig.json
{
"compilerOptions": {
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"module": "amd",
"noEmitOnError": true,
"removeComments": false,
"target": "es5",
"sourceMap": true,
"declaration": true,
"outFile": "./lib/lib-b.js"
},
"exclude": [
"node_modules",
"!node_modules/@types",
"lib"
]
}
I end up with a declaration file that looks like:
lib\lib-b.d.ts
declare module "guid" {
export class Guid {
static EmptyGuid(): string;
}
}
And finally I set the node module to include the typing info:
lib\package.json
{
"name": "lib-b",
"version": "1.0.0",
"description": "",
"main": "./lib/lib-b.js",
"typings": "./lib/lib-b.d.ts",
"files": [
"lib"
],
"author": "",
"license": "ISC"
}
Now in the app that is using the library I bring it in using npm:
app\package.json
{
"name": "web-app",
"version": "1.0.0",
"description": "",
"main": "./lib/index.html",
"author": "",
"license": "ISC",
"dependencies": {
"lib-b": "file:../LibB"
}
}
And in one of its files I try to use the library like so:
import { Guid } from 'lib-b';
But that gives me the following error:
error TS2306: File '.../WebApp/node_modules/lib-b/lib/lib-b.d.ts' is not a module.
I have been looking around, but most examples I find talk about writing typings for an already existing JS library - I cant find much that talks about using the declaration file from existing typescript code. Should the declaration file be usable in other apps? Is there another way to build reusable typescript libraries?
"file:../LibB"?