Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5145

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T06:31:08+00:00 2024-11-27T06:31:08+00:00

How to add absolute paths to your TypeScript Project

  • 61k

In every project we've got to import something. It can be a file from somewhere else from the project or it can be a node module. Today I'm going to teach you how to become a pro at importing files and import a file the right way.

What do we need?

First, we need a TypeScript project. If you use a JavaScript project, then you need to add TypeScript to it. I'm going to create a new React project with TypeScript using Vite.

  npx create-vite-app example --template react-ts   
Enter fullscreen mode Exit fullscreen mode

Since Vite doesn't install node dependencies automatically, we need to install it ourself.

  cd example npm i   
Enter fullscreen mode Exit fullscreen mode

After it will install all node dependencies in our project, we can open the project and start coding.

Coding an example

If you know what you're doing, you can skip this section and move on to the next section. If you're a beginner, you should continue reading and follow the example.

Let's create a utils folder inside src directory. Inside utils folder create a new file and call it index.ts. It should look like this:

A Vite react typescript project folder content screenshot

Now let's create a function inside index.ts file:

  export const addition = (first: number, second: number): number => {     return first + second; }   
Enter fullscreen mode Exit fullscreen mode

Now create a components folders and move App.tsx inside. After that open App.tsx file and try importing addition function from utils. You'll get something like this:

  import { addition } from '../utils';   
Enter fullscreen mode Exit fullscreen mode

Our goal is to avoid all these ../ dots so we would have a very simple import that we can just copy and paste it without changing something throughout the project.

Creating our first absolute path

Open tsconfig.json file and go into "compilerOptions" section. At the end of the section add the following:

  "paths": {     "@utils/*": ["src/utils/*"] }   
Enter fullscreen mode Exit fullscreen mode

And that is it! You created your first absolute path! You can back to App.tsx and change the import from:

  import { addition } from '../utils';   
Enter fullscreen mode Exit fullscreen mode

to:

  import { addition } from '@utils/index';   
Enter fullscreen mode Exit fullscreen mode

But let's go back for a second. Let's take a part the line that we added inside "paths" section in tsconfig.json file.

  "@utils/*": ["src/utils/*"]   
Enter fullscreen mode Exit fullscreen mode

The name @utils is actually the prefix that we want to call before something we want to import from the utils folder. We can call it however we want. if we change @utils/* to @shrek/*, than every time we import something from the utils folder, it will look like this:

  import { addition } from '@shrek/index';   
Enter fullscreen mode Exit fullscreen mode

Inside the array we have src/utils/*. We actually specify in this array all the files we'd like to import using the prefix we have defined.

And that's basically it! That's how you can add absolute paths into your project and make your imports much more shorter and much more satisfying!


I hope you find this article useful and that you learn something new that you didn't know before. I'm more than happy to reply to any questions that you have on your mind 🙂

beginnerstutorialtypescriptwebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.