简体   繁体   中英

How to import swagger model barrel file in Typescript (Angular)

We are currently automatically generating our API models with swagger and want to import them convenient into our project.

At the moment we import it in the following way: tsconfig.ts

"paths": {
  "apimodel/*": ["frontend/swagger-api-definition/model/*"]
}

Random data-source class:

import { ExampleDtoQuery } from 'apimodel/exampleDtoQuery ';
import { ExampleUserDtosUserManagerDashboardResponseUserProject } from 'apimodel/exampleUserDtosUserManagerDashboardResponseUserProject ';

Names are just random, do not judge them please:)

Now what I would like to do, is to create an alias in the tsconfig and use the barrel file, which is generated in the root. Like so: tsconfig.ts

"paths": {
  "@apimodel": ["frontend/swagger-api-definition/index.ts"]
},

Random data-source class:

import { ExampleDtoQuery } from '@apimodel';
import { ExampleUserDtosUserManagerDashboardResponseUserProject } from '@apimodel ';

But the compiler keeps telling me: Cannot find module '@apimodel'.ts(2307)

Any advice?

The * in the path definition tells the TS compiler to allow imports from anything with the prefix @apimodel/ . You also need to provide a baseUrl or it the TS compiler will not be able to find the barrel file in question. To make it work the way you want you should be able to update the paths and baseUrl definitions to be:

"baseUrl": "./",
"paths": {
  "@apimodel": ["frontend/swagger-api-definition/index.ts"]
},

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