I'm encountering confusion while creating declaration files (d.ts).
For instance, I have developed an NPM package called "a" with a single CommonJS module index.ts:
export interface IPoint {
x: number;
y: number;
}
export default function sub(one: IPoint, two: IPoint): IPoint {
return {
x: one.x - two.x,
y: one.y - two.y
};
}
After compiling it, the generated a.d.ts looks like this:
export interface IPoint {
x: number;
y: number;
}
export default function sub(one: IPoint, two: IPoint): IPoint;
It seems that the compiler struggles to create valid d.ts for CommonJS. One must resort to utilities like dts-generator or manually wrapping:
declare module "a"
{
export interface IPoint {
x: number;
y: number;
}
export default function sub(one: IPoint, two: IPoint): IPoint;
}
Now, let's move on to developing package "b" which has a dependency on "a":
/// <reference path="node_modules/a/a.d.ts" />
import sub from "a"
import { IPoint } from "a"
export { IPoint }
export default function distance(one: IPoint, two: IPoint): number {
var s = sub(one, two);
return Math.sqrt(s.x * s.x + s.y * s.y);
}
So far, everything seems to be functioning correctly. Next, I aim to create a package "c" that depends on "b" (and so forth).
How should I specify dependencies in the "b" module (link to "a.d.ts")? Should I try to specify them within "node_modules"? Or should I copy "a.d.ts" to the "/typings/" directory within the "b" package? And then copy "a.d.ts" and "b.d.ts" to "/typings/" in the "c" package (and beyond)?
What is the significance of the "typings" section in "package.json"? In "b/package.json", I have written:
"typings": "./b.d.ts"
This results in an error when compiling "c":
Exported external package typings file 'node_modules/b/b.d.ts' is not a module.
- How can I efficiently create d.ts files for CommonJS modules without having to manually write "declare module", "reference" statements, etc.?