Search code examples
typescriptcommonjs

Importing module and avoid prepending the name


I use TypeScript together with CommonJS module loader.

I have one file called Util.ts and another called Main.ts

I export a class in the Util.ts file

 export class Set {

 }

And I import it in my Main.ts

import * as Util from "./Util";

But in my main file I have to write new Util.Set() in order to access the Set class. Is there a way such that I can avoid prepending the Util identifier? It would be nice if I could write new Set().


Solution

  • You can import the class directly like this:

    import {Set} from './Util';
    

    If you have more than one value to import from Util, you can do this:

    import {Set, MyOtherClass} from './Util';