Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export declaration file for JavaScript function in TypeScript

I'm new to TypeScript and I want to know how to write declaration file for custom JavaScript function. I tried this, but it give me error "Could not find a declaration file for module './main'." and don't give any IntelliSense in index.ts file.

main.js

var maths={ sum:function (a,b){ console.log(a+b)}}

module.exports=maths;

index.ts

import * as mt from "./main"
mt.sum(2,5);

type.d.ts

 declare module 'main' {
export function sum(a:number,b:number):void }

`

like image 973
Chnkz Avatar asked Jun 21 '26 20:06

Chnkz


1 Answers

This import

import * as mt from "./main"

is looking for either main.ts or main.d.ts in the same directory as index.ts.

So one thing you can try is to rename type.d.ts to main.d.ts and remove declare module 'main' from it, leave just export function sum at the top level. When imported that way, module name is taken from the file name. Also, you might have to remove reference to type.d.ts from your project (or tsconfig.json) if you have one.

ALternatively, you can try to change the import to

import * as mt from "main"

so that imported module name matches exactly the name given in declare module.

like image 64
artem Avatar answered Jun 23 '26 08:06

artem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!