chore: fix type inference with different ways to define functions

Signed-off-by: benjdlambert <ben@blam.sh>
This commit is contained in:
benjdlambert
2025-03-19 08:41:50 +01:00
parent 497d47aafa
commit d48525c9f8
13 changed files with 108 additions and 159 deletions
@@ -14,8 +14,11 @@
* limitations under the License.
*/
import { JsonValue } from '@backstage/types';
import { CreatedTemplateFilter, TemplateFilterSchema } from './types';
import {
CreatedTemplateFilter,
TemplateFilterExample,
ZodFunctionSchema,
} from './types';
import { z } from 'zod';
/**
@@ -23,10 +26,12 @@ import { z } from 'zod';
* @alpha
*/
export const createTemplateFilter = <
TSchema extends TemplateFilterSchema<any, any> | undefined,
TFilterSchema extends TSchema extends TemplateFilterSchema<any, any>
? z.infer<ReturnType<TSchema>>
: (arg: JsonValue, ...rest: JsonValue[]) => JsonValue | undefined,
>(
filter: CreatedTemplateFilter<TSchema, TFilterSchema>,
): CreatedTemplateFilter<TSchema, TFilterSchema> => filter;
TFunctionArgs extends [z.ZodTypeAny, ...z.ZodTypeAny[]],
TReturnType extends z.ZodTypeAny,
>(options: {
id: string;
description?: string;
examples?: TemplateFilterExample[];
schema?: ZodFunctionSchema<TFunctionArgs, TReturnType>;
filter: (...args: z.infer<z.ZodTuple<TFunctionArgs>>) => z.infer<TReturnType>;
}): CreatedTemplateFilter<TFunctionArgs, TReturnType> => options;
@@ -14,21 +14,9 @@
* limitations under the License.
*/
import { z } from 'zod';
import { TemplateFilter } from '../../types';
import { JsonValue } from '@backstage/types';
export type { TemplateFilter } from '../../types';
/** @alpha */
export type TemplateFilterSchema<
Args extends z.ZodTuple<
| [z.ZodType<JsonValue>]
| [z.ZodType<JsonValue>, ...(z.ZodType<JsonValue> | z.ZodUnknown)[]],
z.ZodType<JsonValue> | z.ZodUnknown | null
>,
Result extends z.ZodType<JsonValue> | z.ZodUndefined,
> = (zod: typeof z) => z.ZodFunction<Args, Result>;
/** @alpha */
export type TemplateFilterExample = {
description?: string;
@@ -36,25 +24,29 @@ export type TemplateFilterExample = {
notes?: string;
};
/**
* Type for template filter schema functions that supports both Zod function definition methods:
* @alpha
*/
export type ZodFunctionSchema<
TFunctionArgs extends [z.ZodTypeAny, ...z.ZodTypeAny[]],
TReturnType extends z.ZodTypeAny,
> = (
zod: typeof z,
) =>
| z.ZodFunction<z.ZodTuple<TFunctionArgs, null>, TReturnType>
| z.ZodType<
(...args: z.infer<z.ZodTuple<TFunctionArgs>>) => z.infer<TReturnType>
>;
/** @alpha */
export type CreatedTemplateFilter<
TSchema extends
| TemplateFilterSchema<any, any>
| undefined
| unknown = unknown,
TFilterSchema extends TSchema extends TemplateFilterSchema<any, any>
? z.infer<ReturnType<TSchema>>
: TSchema extends unknown
? unknown
: TemplateFilter = TSchema extends TemplateFilterSchema<any, any>
? z.infer<ReturnType<TSchema>>
: TSchema extends unknown
? unknown
: TemplateFilter,
TFunctionArgs extends [z.ZodTypeAny, ...z.ZodTypeAny[]],
TReturnType extends z.ZodTypeAny,
> = {
id: string;
description?: string;
examples?: TemplateFilterExample[];
schema?: TSchema;
filter: TFilterSchema;
schema?: ZodFunctionSchema<TFunctionArgs, TReturnType>;
filter: (...args: z.infer<z.ZodTuple<TFunctionArgs>>) => z.infer<TReturnType>;
};
@@ -18,9 +18,9 @@ import { z } from 'zod';
import {
CreatedTemplateGlobalFunction,
CreatedTemplateGlobalValue,
TemplateGlobalFunctionSchema,
TemplateGlobalFunctionExample,
} from './types';
import { JsonValue } from '@backstage/types';
import { ZodFunctionSchema } from '../filters';
/**
* This function is used to create new template global values in type-safe manner.
@@ -39,10 +39,12 @@ export const createTemplateGlobalValue = (
* @alpha
*/
export const createTemplateGlobalFunction = <
TSchema extends TemplateGlobalFunctionSchema<any, any> | undefined,
TFilterSchema extends TSchema extends TemplateGlobalFunctionSchema<any, any>
? z.infer<ReturnType<TSchema>>
: (...args: JsonValue[]) => JsonValue | undefined,
>(
fn: CreatedTemplateGlobalFunction<TSchema, TFilterSchema>,
): CreatedTemplateGlobalFunction<TSchema, TFilterSchema> => fn;
TFunctionArgs extends [z.ZodTypeAny, ...z.ZodTypeAny[]],
TReturnType extends z.ZodTypeAny,
>(options: {
id: string;
description?: string;
examples?: TemplateGlobalFunctionExample[];
schema?: ZodFunctionSchema<TFunctionArgs, TReturnType>;
fn: (...args: z.infer<z.ZodTuple<TFunctionArgs>>) => z.infer<TReturnType>;
}): CreatedTemplateGlobalFunction<TFunctionArgs, TReturnType> => options;
@@ -15,7 +15,7 @@
*/
import { JsonValue } from '@backstage/types';
import { z } from 'zod';
import { TemplateGlobal } from '../../types';
import { ZodFunctionSchema } from '../filters/types';
export type { TemplateGlobal } from '../../types';
@@ -26,15 +26,6 @@ export type CreatedTemplateGlobalValue<T extends JsonValue = JsonValue> = {
description?: string;
};
/** @alpha */
export type TemplateGlobalFunctionSchema<
Args extends z.ZodTuple<
[] | [z.ZodType<JsonValue>, ...(z.ZodType<JsonValue> | z.ZodUnknown)[]],
z.ZodType<JsonValue> | z.ZodUnknown | null
>,
Result extends z.ZodType<JsonValue> | z.ZodUndefined,
> = (zod: typeof z) => z.ZodFunction<Args, Result>;
/** @alpha */
export type TemplateGlobalFunctionExample = {
description?: string;
@@ -44,31 +35,17 @@ export type TemplateGlobalFunctionExample = {
/** @alpha */
export type CreatedTemplateGlobalFunction<
TSchema extends
| TemplateGlobalFunctionSchema<any, any>
| undefined
| unknown = unknown,
TFilterSchema extends TSchema extends TemplateGlobalFunctionSchema<any, any>
? z.infer<ReturnType<TSchema>>
: TSchema extends unknown
? unknown
: Exclude<
TemplateGlobal,
JsonValue
> = TSchema extends TemplateGlobalFunctionSchema<any, any>
? z.infer<ReturnType<TSchema>>
: TSchema extends unknown
? unknown
: Exclude<TemplateGlobal, JsonValue>,
TFunctionArgs extends [z.ZodTypeAny, ...z.ZodTypeAny[]],
TReturnType extends z.ZodTypeAny,
> = {
id: string;
description?: string;
examples?: TemplateGlobalFunctionExample[];
schema?: TSchema;
fn: TFilterSchema;
schema?: ZodFunctionSchema<TFunctionArgs, TReturnType>;
fn: (...args: z.infer<z.ZodTuple<TFunctionArgs>>) => z.infer<TReturnType>;
};
/** @alpha */
export type CreatedTemplateGlobal =
| CreatedTemplateGlobalValue
| CreatedTemplateGlobalFunction<unknown, unknown>;
| CreatedTemplateGlobalFunction<any, any>;
+1 -3
View File
@@ -73,9 +73,7 @@ export const scaffolderTaskBrokerExtensionPoint =
*/
export interface ScaffolderTemplatingExtensionPoint {
addTemplateFilters(
filters:
| Record<string, TemplateFilter>
| CreatedTemplateFilter<unknown, unknown>[],
filters: Record<string, TemplateFilter> | CreatedTemplateFilter<any, any>[],
): void;
addTemplateGlobals(