question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prefer namespace imports

See original GitHub issue

Prefer namespace imports when importing more than n exports from a module.

// bad
import {map, reduce, filter} from 'lodash';

// good 
import * as _ from 'lodash';

Why?

Mainly git diff results. With namespace imports we don’t have to touch the importing line.

It’s also making the namespace variable more discoverable using the code-completion tools. Hitting _. will prompt all exports of lodash in editors that support interlisense

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mohsen1commented, Jul 11, 2017

it also makes treeshaking/dead code removal not work as well

That’s not true. Webpack and Rollup both can tree shake namespace imports and in case non-esModule modules destructuring won’t help the tree-shaking either.

The diff with import destructuring is not minimal. Users have to add a new line regardless.

Laziness is very subjective also.

2reactions
jasonkuhrtcommented, Oct 26, 2018

I think namesapces can play a helpful role in making code more readable. Its surprisingly relatively difficult to get a straight story on the subject of tree-shaking behaviour in various scenario across bundlers. fwiw I put up some of my own demos here https://github.com/jasonkuhrt/tree-shaking-demos to tree-shake (ahem) some of my FUD. Its a start, not exhaustive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code Syntax Style: Namespace Imports - JetBrains Rider
Prefer fully qualified using name at nested scope — if this checkbox is selected, fully qualified names for imported types are preferred in ......
Read more >
Named namespace imports. If you're organising… | - Medium
The downside to named namespace imports is that you have to define an intermediary module to import and then re-export the namespace. I...
Read more >
Prefer namespace imports for ESM modules #44569 - GitHub
I personally prefer default imports when importing commonjs, when not using named imports. that said, assert as a function is also more ...
Read more >
prefer-namespace-keyword - TypeScript ESLint
Namespaces are an outdated way to organize TypeScript code. ES2015 module syntax is now preferred ( import / export ). For projects still...
Read more >
Documentation - Namespaces - TypeScript
Another way that you can simplify working with namespaces is to use import q = x.y.z to create shorter names for commonly-used objects....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found