any
,其中应为同一对象中定义的ZOD模式的形状。这可能吗?这是一个最小的例子:
import { ComponentType } from "react";
import { z } from "zod";
type Step<
TKey = string,
TSchema extends z.ZodType = z.ZodType,
TProps = unknown
> = {
component: ComponentType<TProps>;
schema: TSchema;
getNextStep?: (data: z.infer<TSchema>) => TKey;
};
type Config<TConfig> = {
[K in keyof TConfig]: TConfig[K] extends Step ? TConfig[K] : unknown;
};
function createConfig<TConfig>(config: Config<TConfig>) {
return config;
}
const config = createConfig({
Step1: {
component: ({ orgId }: { orgId: string }) => null,
schema: z.object({ userId: z.string() }),
getNextStep: (d) => "Step2", // <---- `d` is inferred as `any`
},
Step2: {
component: ({ userId }: { userId: string }) => null,
schema: z.unknown(),
getNextStep: () => "Step1",
},
});
您可以尝试更简单的东西:
import type { ComponentType } from "react";
import { z } from "zod";
type Step<
TKey = string,
TSchema extends z.ZodType = z.ZodType,
TProps = unknown
> = {
component: ComponentType<TProps>;
schema: TSchema;
getNextStep?: (data: TSchema) => TKey;
};
const step1Schema = z.object({ userId: z.string() });
const step2Schema = z.unknown();
type Config = {
Step1: Step<"Step1", typeof step1Schema, { orgId: string }>,
Step2: Step<"Step2", typeof step2Schema, { orgId: string }>,
}
const config: Config = {
Step1: {
component: ({ orgId }) => null,
schema: step1Schema,
getNextStep: (data) => "Step1"
},
Step2: {
component: ({ orgId }) => null,
schema: step2Schema,
getNextStep: (data) => "Step2"
}
}