0

In my node.js app, I have a TypeScript file (util.ts) that defines methods:

export function myFunc() { console.log(42); }

And I use it in another typescript file:

const util = require('./util');
util.myFunc();

This works fine, but the type of util const is any. How can I make it typed, in a way that when I type util., my editor will know how to auto-complete, and when I write:

util.myOtherFunc();

TypeScript compiler will fail and say that util doesn't have a myOtherFunc method?

1 Answer 1

1

How can I make it typed

Instead of :

const util = require('./util');

Do :

import util = require('./util');

More

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Sign up to request clarification or add additional context in comments.

1 Comment

I tried using import, but it failed. Further checking revealed that because our TypeScript project was ported from JavaScript, we were still using the node.js syntax exports.myFunc (and not as I wrote in my question), and when I tried to import it, I got "error TS2306: File 'util.ts' is not a module.". I now realize that I need to use the TypeScript syntax "export function" for the TypeScript compiler to consider this as a module and transform it to the "exports.myFunc =" form. Thanks for answering!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.