admin管理员组文章数量:1296468
I have a simple helper function to capture a type for a configuration object. The configuration object has a couple restrictions:
- The getNextStep function may only return a key for another step in the configuration. (This is working).
- Each configuration step contains an optional zod schema.
I need to infer the type of the zod schema so that getNextStep is aware of the data for that particular step. No matter how I try to capture the generic for the zod schema, it always resolves to z.ZodType
import { z } from "zod";
type Config<TKey extends string> = {
[K in TKey]: {
id: string;
getNextStep?: (
data?: unknown <--- Infer this type from the provided schema, if available.
) => TKey extends infer U extends string ? Exclude<U, K> | null : never;
schema?: z.ZodType;
};
};
function createConfig<TKey extends string>(config: Config<TKey>) {
return config;
}
const Config = createConfig({
Step1: {
id: "Step1",
schema: z.object({ name: z.string() }),
getNextStep: () => "Step2",
},
Step2: {
id: "Step2",
schema: z.object({ enabled: z.boolean() }),
},
});
I have a simple helper function to capture a type for a configuration object. The configuration object has a couple restrictions:
- The getNextStep function may only return a key for another step in the configuration. (This is working).
- Each configuration step contains an optional zod schema.
I need to infer the type of the zod schema so that getNextStep is aware of the data for that particular step. No matter how I try to capture the generic for the zod schema, it always resolves to z.ZodType
import { z } from "zod";
type Config<TKey extends string> = {
[K in TKey]: {
id: string;
getNextStep?: (
data?: unknown <--- Infer this type from the provided schema, if available.
) => TKey extends infer U extends string ? Exclude<U, K> | null : never;
schema?: z.ZodType;
};
};
function createConfig<TKey extends string>(config: Config<TKey>) {
return config;
}
const Config = createConfig({
Step1: {
id: "Step1",
schema: z.object({ name: z.string() }),
getNextStep: () => "Step2",
},
Step2: {
id: "Step2",
schema: z.object({ enabled: z.boolean() }),
},
});
Share
Improve this question
asked Feb 11 at 19:23
Moussa HarajliMoussa Harajli
1,5165 gold badges22 silver badges40 bronze badges
1 Answer
Reset to default 0I was able to achieve what I wanted with the following type:
import { z } from "zod";
function createConfig<
TSchema extends z.ZodType,
TConfig extends Record<string, TSchema>
>(config: {
[K in keyof TConfig]: {
id: string;
schema?: TConfig[K];
getNextStep?: (
data?: z.infer<TConfig[K]>
) => Exclude<keyof TConfig, K> | null;
};
}) {
return config;
}
const Config = createConfig({
Overview: {
id: "Overview",
schema: z.object({ name: z.string() }),
getNextStep: (data) => "Main",
},
Main: {
id: "Main",
schema: z.object({ enabled: z.boolean() }),
},
});
本文标签: genericsTypescript Infer Zod schema type in nested objectStack Overflow
版权声明:本文标题:generics - Typescript: Infer Zod schema type in nested object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639107a2389811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论