Skip to content

Commit 505b92b

Browse files
authored
Merge pull request #2897 from hey-api/copilot/fix-plugininstance-foreach-typing
2 parents cec4739 + a625980 commit 505b92b

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

.changeset/wicked-comics-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix(types): use unique generic names in `PluginInstance` to avoid typing issues

packages/openapi-ts/src/plugins/shared/utils/instance.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
115115
* This ensures, for example, that schemas are always processed before
116116
* operations, which may reference them.
117117
*
118-
* @template T - The event type(s) to yield. Defaults to all event types.
118+
* @template TKind - The event type(s) to yield. Defaults to all event types.
119119
* @param events - The event types to walk over. If none are provided, all event types are included.
120120
* @param callback - Function to execute for each event.
121121
*
@@ -129,40 +129,40 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
129129
* }
130130
* });
131131
*/
132-
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
132+
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
133133
...args: [
134-
...events: ReadonlyArray<T>,
135-
callback: (event: WalkEvent<T>) => void,
134+
...events: ReadonlyArray<TKind>,
135+
callback: (event: WalkEvent<TKind>) => void,
136136
]
137137
): void;
138-
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
138+
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
139139
...args: [
140-
...events: ReadonlyArray<T>,
141-
callback: (event: WalkEvent<T>) => void,
142-
options: WalkOptions<T>,
140+
...events: ReadonlyArray<TKind>,
141+
callback: (event: WalkEvent<TKind>) => void,
142+
options: WalkOptions<TKind>,
143143
]
144144
): void;
145-
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
145+
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
146146
...args: [
147-
...events: ReadonlyArray<T>,
148-
callback: (event: WalkEvent<T>) => void,
147+
...events: ReadonlyArray<TKind>,
148+
callback: (event: WalkEvent<TKind>) => void,
149149
options: any,
150150
]
151151
): void {
152152
if (!this.context.graph) {
153153
throw new Error('No graph available in context');
154154
}
155155

156-
let callback: (event: WalkEvent<T>) => void;
157-
let events: ReadonlyArray<T>;
158-
let options: WalkOptions<T> = {
156+
let callback: (event: WalkEvent<TKind>) => void;
157+
let events: ReadonlyArray<TKind>;
158+
let options: WalkOptions<TKind> = {
159159
getPointerPriority: getIrPointerPriority,
160160
// default functions operate on the full union of kinds; cast them
161161
// to the WalkOptions generic to keep strict typing for callers.
162162
matchPointerToGroup:
163-
matchIrPointerToGroup as unknown as MatchPointerToGroupFn<T>,
163+
matchIrPointerToGroup as unknown as MatchPointerToGroupFn<TKind>,
164164
order: 'topological',
165-
preferGroups: preferGroups as unknown as ReadonlyArray<T>,
165+
preferGroups: preferGroups as unknown as ReadonlyArray<TKind>,
166166
};
167167
if (typeof args[args.length - 1] === 'function') {
168168
events = args.slice(0, -1);
@@ -241,7 +241,7 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
241241
}
242242
if (event) {
243243
try {
244-
callback(event as WalkEvent<T>);
244+
callback(event as WalkEvent<TKind>);
245245
} catch (error) {
246246
this.forEachError(error, event);
247247
}
@@ -259,9 +259,11 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
259259
* @param name Plugin name as defined in the configuration.
260260
* @returns The plugin instance if found, undefined otherwise.
261261
*/
262-
getPlugin<T extends keyof PluginConfigMap>(
263-
name: T,
264-
): T extends any ? PluginInstance<PluginConfigMap[T]> | undefined : never {
262+
getPlugin<TName extends keyof PluginConfigMap>(
263+
name: TName,
264+
): TName extends any
265+
? PluginInstance<PluginConfigMap[TName]> | undefined
266+
: never {
265267
return this.context.plugins[name] as any;
266268
}
267269

@@ -273,9 +275,9 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
273275
* @param name Plugin name as defined in the configuration.
274276
* @returns The plugin instance if found, throw otherwise.
275277
*/
276-
getPluginOrThrow<T extends keyof PluginConfigMap>(
277-
name: T,
278-
): T extends any ? PluginInstance<PluginConfigMap[T]> : never {
278+
getPluginOrThrow<TName extends keyof PluginConfigMap>(
279+
name: TName,
280+
): TName extends any ? PluginInstance<PluginConfigMap[TName]> : never {
279281
const plugin = this.getPlugin(name);
280282
if (!plugin) throw new Error(`plugin not found ${name}`);
281283
return plugin as any;

0 commit comments

Comments
 (0)