Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import * vs import { specificName } in Typescript/ES6?

Declaration

declare module "MyModule" {

export function Foo() {...}
export function Bar() {...}

}

I just need Foo somewhere, how should I import it ?

import * as MyModule from "MyModule";

MyModule.Foo();

or

import {Foo} from "MyModule";
Foo()

Which one is better than another ? Are there any performance implications of importing all the exports in the first way?

Some references which I read before posting questions:

https://www.exratione.com/2015/12/es6-use-of-import-property-from-module-is-not-a-great-plan/

like image 549
Harshil Lodhi Avatar asked Oct 15 '16 15:10

Harshil Lodhi


1 Answers

Importing only what is necessary to your code is, of course, good practice. Say someone writes some thousand-line code importing everything, and then you try to analyze it. Do you think you would easily know which functions used in your code are imported or which is not? Obviously it is dubious and bad practice.

With regards performance, I suppose not much affected.

like image 55
Rax Weber Avatar answered Oct 20 '22 22:10

Rax Weber