diff --git a/.changeset/wicked-comics-hang.md b/.changeset/wicked-comics-hang.md new file mode 100644 index 0000000000..6c983dcc1b --- /dev/null +++ b/.changeset/wicked-comics-hang.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix(types): use unique generic names in `PluginInstance` to avoid typing issues diff --git a/packages/openapi-ts/src/plugins/shared/utils/instance.ts b/packages/openapi-ts/src/plugins/shared/utils/instance.ts index 453316440e..3de70f2e63 100644 --- a/packages/openapi-ts/src/plugins/shared/utils/instance.ts +++ b/packages/openapi-ts/src/plugins/shared/utils/instance.ts @@ -115,7 +115,7 @@ export class PluginInstance { * This ensures, for example, that schemas are always processed before * operations, which may reference them. * - * @template T - The event type(s) to yield. Defaults to all event types. + * @template TKind - The event type(s) to yield. Defaults to all event types. * @param events - The event types to walk over. If none are provided, all event types are included. * @param callback - Function to execute for each event. * @@ -129,23 +129,23 @@ export class PluginInstance { * } * }); */ - forEach( + forEach( ...args: [ - ...events: ReadonlyArray, - callback: (event: WalkEvent) => void, + ...events: ReadonlyArray, + callback: (event: WalkEvent) => void, ] ): void; - forEach( + forEach( ...args: [ - ...events: ReadonlyArray, - callback: (event: WalkEvent) => void, - options: WalkOptions, + ...events: ReadonlyArray, + callback: (event: WalkEvent) => void, + options: WalkOptions, ] ): void; - forEach( + forEach( ...args: [ - ...events: ReadonlyArray, - callback: (event: WalkEvent) => void, + ...events: ReadonlyArray, + callback: (event: WalkEvent) => void, options: any, ] ): void { @@ -153,16 +153,16 @@ export class PluginInstance { throw new Error('No graph available in context'); } - let callback: (event: WalkEvent) => void; - let events: ReadonlyArray; - let options: WalkOptions = { + let callback: (event: WalkEvent) => void; + let events: ReadonlyArray; + let options: WalkOptions = { getPointerPriority: getIrPointerPriority, // default functions operate on the full union of kinds; cast them // to the WalkOptions generic to keep strict typing for callers. matchPointerToGroup: - matchIrPointerToGroup as unknown as MatchPointerToGroupFn, + matchIrPointerToGroup as unknown as MatchPointerToGroupFn, order: 'topological', - preferGroups: preferGroups as unknown as ReadonlyArray, + preferGroups: preferGroups as unknown as ReadonlyArray, }; if (typeof args[args.length - 1] === 'function') { events = args.slice(0, -1); @@ -241,7 +241,7 @@ export class PluginInstance { } if (event) { try { - callback(event as WalkEvent); + callback(event as WalkEvent); } catch (error) { this.forEachError(error, event); } @@ -259,9 +259,11 @@ export class PluginInstance { * @param name Plugin name as defined in the configuration. * @returns The plugin instance if found, undefined otherwise. */ - getPlugin( - name: T, - ): T extends any ? PluginInstance | undefined : never { + getPlugin( + name: TName, + ): TName extends any + ? PluginInstance | undefined + : never { return this.context.plugins[name] as any; } @@ -273,9 +275,9 @@ export class PluginInstance { * @param name Plugin name as defined in the configuration. * @returns The plugin instance if found, throw otherwise. */ - getPluginOrThrow( - name: T, - ): T extends any ? PluginInstance : never { + getPluginOrThrow( + name: TName, + ): TName extends any ? PluginInstance : never { const plugin = this.getPlugin(name); if (!plugin) throw new Error(`plugin not found ${name}`); return plugin as any;