简体   繁体   中英

How to configure paths property in tsconfig.json file for external libraries

The closest explanation for what I am trying to do was found here [a link] . Still I am struggling to get my solution to compile.

I have external typings which I would like to use in my project and all of them has .d.ts files with namespace "DS/ " which I store in a folder called typings. Below is my folder structure for my simple Angular app.

 -MyAppName --typings ----all my modules d.ts --src ----app -tsconfig.json 

this is my tsconfig.json file look like 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": { "DS/*": [ "typings/*" ], "*": [ "src/*" ] } }, "include": [ "typings/**/*.ts" ], "exclude": [ "node_modules" ], "angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictInjectionParameters": true } 

I get an error for importing my library as below import { MyModuleName } from 'DS/MyModuleName';

ERROR in ./src/app/app.module.ts Module not found: Error: Can't resolve 'DS/MyModuleName' in 'MyAppName\\src\\app'

I'm using AngularCLI to build the app. I have failed to configure my tsconfig file to support these externally imported libraries. Not sure what I am doing wrong. Please help.

according to your tree, try in your tsconfig.ts

"typeRoots": ["node_modules/@types", "typings" ]...

"paths": {
  "DS/*": [
    "typings/*"
  ],
}

"include": ["src", "typings"],

then in your app.module.ts

import { MyModuleName} from 'DS/myModuleName.module';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM