splintor
splintor

Reputation: 10154

How to properly export TypeScript methods in node.js app

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?

Upvotes: 0

Views: 46

Answers (1)

basarat
basarat

Reputation: 276393

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

Upvotes: 1

Related Questions