Somkun
Somkun

Reputation: 109

Importing an exported function with name in Typescript

I know that for classes in an export, you can singularly grab classes like so:

import {classA, classB} from "largeExport";

However given an export that contains a function, where it would be used as such:

var sum = require("myAdder")(1, 2)

How do you import it with it's own name?

I know you can import the whole thing and use the function like this:

import * as adder from "myAdder";
var sum = adder(1, 2);

however the export that I'm working with is actually quite big, and importing the whole thing is less than ideal.

Upvotes: 0

Views: 2078

Answers (1)

Dan
Dan

Reputation: 2858

its not about "classes" but the way it's been exported

if it was exported as

export const x = "x";
export class X {};

then

import {x, X} from "./x";

or

import * as xxx from "./x"
import x = xxx.x;
import X = xxx.X;

if

const x = "x";
export default x;
export class X {};

then

import x from "./x"

or

import x, {X} from "./x"

and you will be importing the whole file anyway

if you are concerned about import sizes you might want to split you module into many, more modular files.

Upvotes: 1

Related Questions