YousefED / typescript-json-schema

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

Filter symbols by both name and fully qualified name #580

Open steven87vt opened 11 months ago

steven87vt commented 11 months ago

According to the README you are supposed to be able to filter by fully qualified type name, however I cannot see how it would operate from looking at the code.

I would want to use this during cli to generate the correct type given multiple types by the same name. This is happening because of custom file extension support vue. Excluding all .vue files has no effect on the issue. So in my case I just want to filter any type not mine.

Example:

//node_modules/@vue/runtime-core/dist/runtime-core.d.ts
export interface AppConfig {
  ...
  // going to get sucked up by anything that includes a .vue file because of default definition files under node_modules
}

//my_project/src/types.ts
export interface AppConfig {
  some_item: string
}
npx typescript-json-schema my_project/tsconfig.app.json '"/my_project/src/store/types".AppConfig' --uniqueNames

Error: 2 definitions found for requested type ""/my_project/src/store/types".AppConfig".

Looking at #151 symbols are filtered by the type name value. But should it be filtered by either type name or fully qualified name?

https://github.com/YousefED/typescript-json-schema/blob/ec3f0982771a04a032f68351728b85bcb4480726/typescript-json-schema.ts#L1576

public getSymbols(name?: string): SymbolRef[] {
  if (name === void 0) {
    return this.symbols;
  }

  return this.symbols.filter(function (symbol) { return symbol.typeName === name || symbol.fullyQualifiedName === name; });
  // return this.symbols.filter(function (symbol) { return symbol.typeName === name });
}

After making that change locally the cli generated the schema properly.