<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Justin Dwyer</title>
    <description>The latest articles on DEV Community by Justin Dwyer (@justindwyer6).</description>
    <link>https://dev.to/justindwyer6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1036503%2F940ecc84-bc8f-45e1-87e8-739ff14016e7.jpeg</url>
      <title>DEV Community: Justin Dwyer</title>
      <link>https://dev.to/justindwyer6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justindwyer6"/>
    <language>en</language>
    <item>
      <title>What import alias would you like configured?</title>
      <dc:creator>Justin Dwyer</dc:creator>
      <pubDate>Wed, 01 Mar 2023 18:02:04 +0000</pubDate>
      <link>https://dev.to/justindwyer6/what-import-alias-would-you-like-configured-51n4</link>
      <guid>https://dev.to/justindwyer6/what-import-alias-would-you-like-configured-51n4</guid>
      <description>&lt;p&gt;I'm building my personal website with Next.js and I came across this question after running &lt;code&gt;yarn create next-app --typescript&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;? What import alias would you like configured? › @/*&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1qcgzcnhh2ro8z1hslp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1qcgzcnhh2ro8z1hslp8.png" alt="Screenshot of my terminal while going through the create-next-app wizard" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm familiar with the concept of &lt;a href="https://nextjs.org/docs/advanced-features/module-path-aliases" rel="noopener noreferrer"&gt;import aliases&lt;/a&gt;, but I didn't know how Next expected this question to be answered. Usually, I use multiple import aliases. I might use &lt;code&gt;@styles&lt;/code&gt;, &lt;code&gt;@components&lt;/code&gt;, &lt;code&gt;@hooks&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;So I decided to experiment. I typed &lt;code&gt;@components&lt;/code&gt; into the prompt, and I got this response back:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;› Import alias must follow the pattern &amp;lt;prefix&amp;gt;/*&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Got it. So then I tried &lt;code&gt;@components/*&lt;/code&gt;, and my app was created. I checked my &lt;code&gt;tsconfig.json&lt;/code&gt; (or &lt;code&gt;jsconfig.json&lt;/code&gt;) and compared it to an app that used the default &lt;code&gt;@/*&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// tsconfig.json with default (@/*)
{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "@/*": ["./*"]
    }
  },
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// tsconfig.json with test (@components/*)
{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "@components/*": ["./*"]
    }
  },
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What came out of the test is not what I wanted. In the past, I've used a pattern like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// tsconfig.json
{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "@components/*": ["components/*"],
      "@styles/*": ["styles/*"],
      "@hooks/*": ["hooks/*"]
    }
  },
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which allowed me to reference imports like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Button from '@components/Button';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the default (&lt;code&gt;"@/*": ["./*"]&lt;/code&gt;) provided by the Next.js setup wizard allows me to do the same thing without having to explicitly set each path in my &lt;code&gt;tsconfig.json&lt;/code&gt; — I just need to add an extra &lt;code&gt;/&lt;/code&gt; after the &lt;code&gt;@&lt;/code&gt; to my import path, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Button from '@/components/Button';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps my &lt;code&gt;tsconfig.json&lt;/code&gt; from getting bloated and hard to maintain (compared to the pattern I used to use). Cool!&lt;/p&gt;

&lt;p&gt;The only use case I can think of for modifying this default is if you are using an &lt;code&gt;src/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Do you have a use case I'm not thinking of? Leave a comment if so!&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>certification</category>
      <category>career</category>
    </item>
  </channel>
</rss>
