I have a project with Angular 6, Typescript, webpack 4.
I want to use alias in webpack in order to make my import statement clearer.
This is the code I have in webpack:
resolve: {
alias: {
assets: `${helpers.root('src/assets/')}`,
app: `${helpers.root('src/app/')}`
},
}
The "assets" alias is working fine !
I use the "app" alias in this way within the code of a component:
import { UtilsService } from 'app/_services/utils.service';
And this is the constructor of the component:
constructor( @Inject( UtilsService ) private utils: UtilsService, ) {
}
I don't have an error on the import statement, so I guess that the alias works in some way.
But I have the following error in the constructor:
TS2709: Cannot use namespace 'UtilsService' as a type.
I must be missing some configuration somewhere but can't figure out what ?
Any help will be greatly appreciated
EDIT: Following kemsky and David comment this is what I tried:
I added the following in tsconfig.json:
"baseUrl": ".",
"paths": {
"@app": ["src/app/"] // This mapping is relative to "baseUrl"
}
I modify webpack.config.js to have:
resolve: {
alias: {
assets: `${helpers.root('src/assets/')}`,
'@app': `${helpers.root('src/app/')}`
},
}
And I tried to import using:
import { UtilsService } from 'app/_services/utils.service';
I still have the same error in the constructor of my component:
constructor( private utils: UtilsService, ) {}
TS2709: Cannot use namespace 'UtilsService' as a type.
If I change the import statement to something that don't exist, I have a the following error:
Module not found: Error: Can't resolve '@DONT_EXIST/_services/utils.service'
Which makes me think that the problem is with tsconfig.
Note:
I'm using awesome-typescript-loader, maybe it's linked, this is how I load tsconfig.json inside webpack.config.js
module: {
rules: [
{
enforce: 'pre',
test: /\.ts$/,
use: [
{
loader: 'tslint-loader'
}
]
},
{
test: /\.ts$/,
use: [
{
loader: '@angularclass/hmr-loader',
options: {
pretty: !isProd,
prod: isProd
}
},
{
loader: 'ng-router-loader',
options: {
loader: 'async-import',
genDir: 'compiled',
aot: AOT
}
},
{
loader: 'awesome-typescript-loader',
options: {
configFileName: 'tsconfig.json',
useCache: !isProd
}
},
{
loader: 'angular2-template-loader'
}
],
exclude: [/\.(spec|e2e)\.ts$/]
},