Auto-imports / import shortcuts / namespaces #113
Replies: 4 comments 11 replies
-
Looks cool! I think this would be fun to play with. An alternate compilation (which I think you've written in Discord at some point) is this: import {addDays} from 'date-fns'
import add from 'lodash/add'
import foo from '../utils/foo'
addDays(a, b)
add(c, d)
foo(e, f) I guess this would be more efficient (less The other concern I have is |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
One possibility might be a
|
Beta Was this translation helpful? Give feedback.
-
I have a problem now with One natural solution is Alternatively we find some other notation, e.g. one that has a clear open and close delimiter. For example, I was thinking For symmetry, another option would be Another approach is to use a trailing character instead of a leading character. For example, date-fns^addDays(a, b) // Named import from node_modules
node:fs/promises^readFile(filename) // Named import from namespaced submodule
lodash^.add(c, d) // Default import (a function) from node_modules
../utils^.add(e, f) // Default import (an object containing a function) from relative module
<ui/Button^> // JSX element (where `ui` could be an ESBuild alias to `src/ui` for example) By the way, |
Beta Was this translation helpful? Give feedback.
-
What?
Civet:
Output:
It's necessary to disambiguate between accessing a named export Vs. accessing a property of the default export, so
#
is used for the named export to resemble how#
is used to point to a specific anchor inside a page on the internet (e.g.: example.com/foo#bar where #bar is something inside that page).Why?
import { format as formatDate } from 'date-fns'
to avoid conflicts with other things in the code. With namespaces, that's not a concern. While namespaces may not be possible, inline imports as showed in the code above are. Why waste any time thinking about name conflicts if we can just not have name conflicts in the first place?format
may not clear on what they do. Is it formatting a date? A phone number? A currency? Knowing where it comes from eliminate that doubt: you look^date-fns.format(value)
and you immediately know what it's about, not even 1 second wondering. Why implicit/ambiguous if we can make it explicit/obvious?Relevant links
DateUtil.res
, it's immediately available in all other modules as theDateUtil
module (file names must be unique), e.g.:DateUtil.format(date)
. No import needed.Edits
Beta Was this translation helpful? Give feedback.
All reactions