这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-comics-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(types): use unique generic names in `PluginInstance` to avoid typing issues
48 changes: 25 additions & 23 deletions packages/openapi-ts/src/plugins/shared/utils/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
* 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.
*
Expand All @@ -129,40 +129,40 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
* }
* });
*/
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
...args: [
...events: ReadonlyArray<T>,
callback: (event: WalkEvent<T>) => void,
...events: ReadonlyArray<TKind>,
callback: (event: WalkEvent<TKind>) => void,
]
): void;
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
...args: [
...events: ReadonlyArray<T>,
callback: (event: WalkEvent<T>) => void,
options: WalkOptions<T>,
...events: ReadonlyArray<TKind>,
callback: (event: WalkEvent<TKind>) => void,
options: WalkOptions<TKind>,
]
): void;
forEach<T extends IrTopLevelKind = IrTopLevelKind>(
forEach<TKind extends IrTopLevelKind = IrTopLevelKind>(
...args: [
...events: ReadonlyArray<T>,
callback: (event: WalkEvent<T>) => void,
...events: ReadonlyArray<TKind>,
callback: (event: WalkEvent<TKind>) => void,
options: any,
]
): void {
if (!this.context.graph) {
throw new Error('No graph available in context');
}

let callback: (event: WalkEvent<T>) => void;
let events: ReadonlyArray<T>;
let options: WalkOptions<T> = {
let callback: (event: WalkEvent<TKind>) => void;
let events: ReadonlyArray<TKind>;
let options: WalkOptions<TKind> = {
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<T>,
matchIrPointerToGroup as unknown as MatchPointerToGroupFn<TKind>,
order: 'topological',
preferGroups: preferGroups as unknown as ReadonlyArray<T>,
preferGroups: preferGroups as unknown as ReadonlyArray<TKind>,
};
if (typeof args[args.length - 1] === 'function') {
events = args.slice(0, -1);
Expand Down Expand Up @@ -241,7 +241,7 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
}
if (event) {
try {
callback(event as WalkEvent<T>);
callback(event as WalkEvent<TKind>);
} catch (error) {
this.forEachError(error, event);
}
Expand All @@ -259,9 +259,11 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
* @param name Plugin name as defined in the configuration.
* @returns The plugin instance if found, undefined otherwise.
*/
getPlugin<T extends keyof PluginConfigMap>(
name: T,
): T extends any ? PluginInstance<PluginConfigMap[T]> | undefined : never {
getPlugin<TName extends keyof PluginConfigMap>(
name: TName,
): TName extends any
? PluginInstance<PluginConfigMap[TName]> | undefined
: never {
return this.context.plugins[name] as any;
}

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