Home > Enterprise >  Typescript can't see declared class on a compile stage but can at the end
Typescript can't see declared class on a compile stage but can at the end

Time:10-05

Typescript can see my class that I declared in .d.ts file, but not when it's doing a compile

I declared a class in .d.ts

declare class Handlebars {
static compile(s: string): (a: object) => string;
}

And added that file to configurations, so main file can see the class

{
"compilerOptions": {
  "allowJs": false
},
"files": [
    ".d.ts",
    "index.ts"
]
}

p1 p2

CodePudding user response:

Try this. You don't import Handlebars so it can't use it.

.d.ts

declare class Handlebars {
    static compile(s: string): (a: object) => string;
}

export { Handlebars };

index.ts

import { Handlebars } from "./.d";

let template = Handlebars.compile("text");
console.log(template({ doesWhat: "rocks" }));
  • Related