Prefer namespace imports
See original GitHub issuePrefer 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:
- Created 6 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.