DEV Community

How to improve imports in your code

Rafael Sant'Ana on July 13, 2021

Hello, there! After a while without new posts, I decided to come back with a really useful tip to simplify your code and reduce repetition. Have ...
Collapse
 
ats1999 profile image
Rahul kumar •

You can do much better, read more about jsconfig and tsconfig

Collapse
 
tsuyusk profile image
Rafael Sant'Ana •

With tsconfig paths ?

Collapse
 
heyprotagonist profile image
Anguram Shanmugam •

you can use decorators also you need to import files in your index.{js,ts}

watch this: youtube.com/shorts/WpgZKBtW_t8?fea...

Thread Thread
 
tsuyusk profile image
Rafael Sant'Ana •

thaaanks

Collapse
 
ats1999 profile image
Rahul kumar •

Learn on your own!

Thread Thread
 
heyprotagonist profile image
Anguram Shanmugam •

oh c'mon 😂

Is Dev.to your app to say a comment like this ¯\(ツ)/¯

Collapse
 
hamodey85 profile image
Mohammed Almajid • • Edited

You can make it much better

I use Typescript all the time and in my tsconfig I set my paths

        "paths": {

            "@core/*": ["src/core/*"],
            "@types/*": ["src/core/types/*"],
            "@config/*": ["src/core/config/*"],
            "@models/*": ["src/models/*"],
            "@middleware/*": ["src/middleware/*"],
            "@controllers/*": ["src/controllers/*"]
        }
Enter fullscreen mode Exit fullscreen mode

but in my application i have to use tsconfig-paths package

so if i want to import files from controllers i do

import { something} from "@controllers/"
Enter fullscreen mode Exit fullscreen mode

resources
npmjs.com/package/tsconfig-paths
typescriptlang.org/docs/handbook/m...
stackoverflow.com/questions/432817...

correct me if I missed something

Collapse
 
heyprotagonist profile image
Anguram Shanmugam •

neat & clean 🤓

Collapse
 
erasmuswill profile image
Wilhelm Erasmus •

How does this affect treeshaking though?