I have several utility functions. What is the best way to package these up, and then import them?
This is what I am trying to do:
import * as util f
There's a couple problems here:
doSomething is an instance methodimport * as util, util represents the module, not an object in it. If you want Util, you should just import that:
import { Util } from './util'
Next, you should instantiate Util, before finally calling the method on it:
var u = new Util();
u.doSomething("test");
Here's your code patched up:
import { Util } from './util'
export class MyClass{
constructor()
{
var u = new Util();
u.doSomething("test");
}
}
All that said, there seems to be something odd about the way you're using your utils. This is totally personal opinion, but I wouldn't invoke methods that "do something", i.e. cause side effects, in a constructor.
Also, the methods in Util don't really look like they need to be in that class, since the class holds no state that they depend on. You can always export regular functions from a module. If you wrote your utils module like this:
export function doSomething(val: string) { return val; }
export function doSomethingElse(val: string) { return val; }
you'd be exporting your functions directly and would sidestep the instantiation hassle, and in fact your original code would work correctly as is.