11

Assume project structure below:

packages/
   utilities/
      index.ts
   app/
      app.ts
tsconfig.json

I created path aliases in order to access packages easier between each other, my tsconfig looks like this

{
  "compilerOptions": {
    "moduleResolution": "node",
    "noEmit": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowJs": false,
    "noImplicitAny": true,
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "$app/*": ["packages/app/*"],
      "$utilities/*": ["packages/utilities/*"],
    }
  },
  "include": [
    "packages/*"
  ]
}

Finally my packages/utilities/index.ts file is sort of a mid point that imports and exports all utils I want to expose i.e.

export { isValidEmail } from '$utilities/validators';
//... etc

I initially thought that my app package would be able to do something like this

import { isValidEmail } from "$utilities";
// Errors Cannot find module '$utilities' or its corresponding type declarations.

But it errors with message above unless I use full path $utilities/index

1
  • the path specifies $utilities/* will be mapped to the corresponding directory, so just $utilities would have to be a separate entry if you want to import it that way. Someone might be able to give a cleaner solution but I think that's just the way it works. Commented Jul 17, 2020 at 15:06

3 Answers 3

35

I did this trick and it solved my problem

"paths": {
    "@services": ["src/services"],
    "@services/*": ["src/services/*"],
    "@/*": ["src/*"]
},

Now I can access src/services/index by this line

import { useUtil } from '@services';

And it also works if I want to access child files/folders

enter image description here

I also noticed that path aliases on top are prioritized (in intellisense & auto imports) than ones that come after. So it's better to put longer aliases on top and shorter ones in the end.

Sign up to request clarification or add additional context in comments.

Comments

1

This is easy to get fixed ! Your tsconfig.json file should look like one of the following cases:

  1. If the folder only contains index.{js/ts} then your tsconfig.json should look like:
{
  ...,
  "compilerOptions": {
    ...,
    "paths": {
      ...,
      "@folder": ["folder"]
    }
  }
}
  1. If the folder contains subfolders only
{
  ...,
  "compilerOptions": {
    ...,
    "paths": {
      ...,
      "@folder/*": ["folder/*"]
    }
  }
}
  1. If the folder contains both index.{js/ts} and subfolders
{
  ...,
  "compilerOptions": {
    ...,
    "paths": {
      ...,
      "@folder": ["folder"],
      "@folder/*": ["folder/*"]
    }
  }
}

That's all !

Comments

0

Since isValidEmail is exported by index.ts, it is working as expected.

In the import statement you have to set the path to the file you want to reference, just like you did in the export statement.

In this case it will be:

import { isValidEmail } from '$utilities/index';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.