0

I come from PHP background and now I'm diving into typescript code.

In PHP with composer's classmap and the composer.json I can declare namespaces PSR-4 style and as long as I import them properly with the USE, it will resolve.

Reading the following: How do I use namespaces with TypeScript external modules?

didn't help me resolve the question: - is there something in Typescript that will help resolve paths of classes instead of dealing with relative path" as currently I have to ../.. all the way to root and then go fetch the classes I created within the app directory.

node_modules does not suffer from this path problem (i.e: I can be in the test folder and call import * as 'restify'; without issues.

azngunit81
  • 1,574
  • 2
  • 20
  • 38

1 Answers1

0

You can set up path aliases in your tsconfig.json. For example, if tsconfig.json had the following:

// ... other config omitted
  "baseUrl": "./src",
  "paths": {
    "components/*": ["components/*"]
  }

Then you could import stuff from src/components like this:

import Whatever from 'components/Whatever';

See documentation on module resolution here: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98