Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript import * without creating aliases

In TypeScript, how do you "import *" from a file without creating any aliases?

E.g. I have a file "utils" with top-level exported functions and want to import all them without recreating the aliases for each function.

Something like this:

import * from "utils";

Is that possible?

like image 212
nino.porcino Avatar asked Jul 25 '15 09:07

nino.porcino


1 Answers

You can't generate an automatic name, you have to give it a name that is local to your file. This is by design to give each file its own naming context.

// import the default export (something exported with "export default")
import Stuff from "./Stuff";

// import specific things only; aliases in this style are optional
import { ItemA as AliasA, ItemB as AliasB } from "./Stuff";
import { ItemA, ItemB } from "./Stuff";

// import everything at once, grouped under a common name
import * as Stuff from "./Stuff";

I ... want to import all them without recreating the aliases for each function.

It sounds like you'd want the third option from above.

but with this syntax creates an alias

I think the idea is that if you could just import everything while taking the names as they are defined, then you'd have naming collisions. The way this works, you're forced to choose a name for every import, leaving it up to the names you choose to avoid collisions rather than having the imports clobber each other.

I think you can do something similar, but only with .d.ts files. jquery.d.ts does it, but I'm not 100% solid on how it works. You can simply say:

// where the file is really Stuff.d.ts
import "./Stuff";
like image 180
Dave Cousineau Avatar answered Oct 13 '22 08:10

Dave Cousineau