'Creating a Types Declaration file for an existing library
I'm working towards building a Type Declaration file for an existing JavaScript library. Since I have no experience with creating declaration files I've created a simple test project that emulates the library I ultimately want to create a declaration file for. I have some TypeScript experience and I've read through the TypeScript documentation on declaration files but I'm struggling to get my test project working. When I try to compile I get the error:
File '<...>/test.d.ts' is not a module.
My test project structure is as follows:
Project
├ index.ts
├ test.d.ts
┕ test.js
test.js
class MyClassA {
constructor(){}
static staticMethodA(x, y) {
console.log(`staticMethodA(${x}, ${y})`);
}
regularMethodA(z) {
return `regularMethodA(${z})`;
}
}
class MyClassB {
constructor(){}
regularMethodB(q) {
return `regularMethodB(${q})`;
}
}
var classes = {
ClassA: MyClassA,
MyClassB: MyClassB
};
exports.classes = classes;
test.d.ts
declare module "test" {
class MyClassA {
constructor();
public static staticMethodA(x: number, y: number): void;
public regularMethodA(z: string): string;
}
class MyClassB {
constructor();
public regularMethodB(q: string): string;
}
export interface classes {
ClassA: MyClassA;
MyClassB: MyClassB;
}
}
index.ts
import {MyClassA} from "./test.js";
const foo: MyClassA = new MyClassA();
console.log(foo.regularMethodA("Test"));
I don't know why I'm getting the not a module error. I'm assuming there's a problem with my test.d.ts file but I don't see what I'm missing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
