YousefED / typescript-json-schema

Generate json-schema from your Typescript sources
BSD 3-Clause "New" or "Revised" License
3.17k stars 323 forks source link

Fail to generate schema when using tsconfig-paths #505

Open marcelo-agil opened 2 years ago

marcelo-agil commented 2 years ago

Hi guys,

I'm using tsconfig-paths to avoid path hell. Instead of importing files using import * from '../../../', it uses an array of path aliases and they current values. Works pretty well.

Example of tsconfig-path:

"paths": {
      "@shared/*": ["src/modules/shared/domain/*"],
}
(Code fixed, thank you erkin98).

The problem is when I try to programmatic use typescript-json-schema, it throws this error: "Cannot find module '@shared/util/util.interface' or its corresponding type declarations.".

The file above constains this code:

import * as IUtil from '@shared/util/util.interface';

export interface IUserAgenda extends IUtil.IDateFields {
  /**
   * Inform if this user has agenda app configured
   *@default false
   */
  isAgendaAppIntegrated?: boolean;
  /**
   * App key
   *
   */
  agendaAppKey?: string;
  /**
   * App secret
   *
   */
  agendaAppSecret?: string;
  /**
   * App endpoint
   *
   */
  agendaAppEndpoint?: string;
}

My question is: How to make typescript-json-schema understand the tsconfig-paths?

erkin98 commented 2 years ago

try changing

"paths": {
      "@sharedDomain/*": ["src/modules/shared/domain/*"],
} 

to


"paths": {
      "@shared/*": ["src/modules/shared/*"],
}
marcelo-agil commented 2 years ago

@erki

try changing

"paths": {
      "@sharedDomain/*": ["src/modules/shared/domain/*"],
} 

to

"paths": {
      "@shared/*": ["src/modules/shared/*"],
}

My mistake on the example. The code is right, but won't work. In the interface file: import * as IUtil from '@shared/util/util.interface';

In tsconfig-paths: "@shared/": ["src/modules/shared/"],

It doesn't recognize. But on runtime code works, file gets imported. If I use full path on import, than typescript-json-schema works.

He is not translating @shared. How can I achieve that?

erkin98 commented 2 years ago

could you specify which command are you running and would you consider pasting your tsconfig?

marcelo-agil commented 2 years ago

could you specify which command are you running and would you consider pasting your tsconfig?

Sure.

Here's my tsconfig.ts:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es6"],
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "typeRoots": ["./node_modules/@types", "./src/@types"],
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "@sharedDomain/*": ["src/modules/shared/domain/*"],
      "@negotiation/*": ["src/modules/user/*"],
      "@notification/*": ["src/modules/notification/*"],
      "@customer/*": ["src/modules/customer/*"],
      "@person/*": ["src/shared/person/*"],
      "@personDomain/*": ["src/modules/shared/person/domain/*"],
      "@personController/*": [
        "src/modules/shared/person/app/http/controller/*"
      ],
      "@personHelper/*": ["src/modules/user/app/http/helper/*"],
      "@security/*": ["src/modules/shared/security/*"],
      "@userDomain/*": ["src/modules/shared/security/domain/user/*"],
      "@product/*": ["src/modules/product/*"],
      "@scheduler/*": ["src/modules/scheduler/*"],
      "@shared/*": ["src/modules/shared/*"],
      "@config/*": ["src/config/*"]
    }
  },
  "include": ["src/**/*", "server.ts"]
}

The command is run through script:

const program = TJS.getProgramFromFiles(
      [
        resolve(
          `./src/modules/shared/security/domain/user/user-agenda.interface.ts`
        ),
      ],
      compilerOptions
    );
    const schema = TJS.generateSchema(program, '*', compilerOptions);

The "Agenda" interface is just a test file:


import * as IUtil from '@shared/util/util.interface';

export interface IUserAgenda extends IUtil.IDateFields {
  /**
   * Inform if this user has agenda app configured
   *@default false
   */
  isAgendaAppIntegrated?: boolean;
  /**
   * App key
   *
   */
  agendaAppKey?: string;
  /**
   * App secret
   *
   */
  agendaAppSecret?: string;
  /**
   * App endpoint
   *
   */
  agendaAppEndpoint?: string;
}

Once again, if I replace the @shared by the path, it works. If I keep @shared on imports, this is the error displayed: " Cannot find module '@shared/util/util.interface' or its corresponding type declarations."

marcelo-agil commented 2 years ago

@erkin98 any thoughts?

erkin98 commented 2 years ago

I suggest you use the typescript-json-schema "tsconfig.json" "*" -o all.json command and add "types": ["folder-inside-src/@types"], to your tsconfig.json

nevadavid commented 10 months ago

Any news on this issue? Because I use v0.62.0 but I get same error with similar context.