Short TypeScript import statement syntax not working in Angular

Ahsan

I am trying to shorten the imports statements in my Angular project i.e

import { AuthenticationService } from '../_services/authentication.service';

to

import { AuthenticationService } from '@app/_service';

I am following an article where the author has used this technique and I did as he described but when I try to serve the application, I am get the error that module '@app/_service is not found in terminal i.e

  src/app/_helpers/jwt-interceptor.ts(6,41): error TS2307: Cannot find module '@app/_services'.

Author described to do the following changes in tsconfig.json

my tsconfig.json is as below.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "paths": {
    "@app/*": ["src/app/*"],
    "@environments/*": ["src/environments/*"]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

I have tried this solution described in this video too but no luck. I am also using index.ts file to export all modules in a directory as described in article.

Short Imports with TypeScript Path Mapping

Any solution for this problem OR what am I doing wrong here?

Ahsan

By changing the "baseUrl": "./" to "baseUrl": "src" and also omitting @ from paths object I am able to import without any error.

{
      "compileOnSave": false,
      "compilerOptions": {
        "baseUrl": "src",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "module": "esnext",
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2015",
        "typeRoots": [
          "node_modules/@types"
        ],
        "lib": [
          "es2018",
          "dom"
        ]
      },
      "paths": {
        "app/*": ["app/*"],
        "environments/*": ["environments/*"]
      },
      "angularCompilerOptions": {
        "fullTemplateTypeCheck": true,
        "strictInjectionParameters": true
      }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related