Replies: 1 comment 1 reply
-
I'm not sure there is any way to fix this. That's what brought me here too. Here's my code: import { parse, type TomlPrimitive } from 'smol-toml';
import { z, ZodError } from 'zod';
import { fromError } from 'zod-validation-error';
export async function loadConfig<TZodType extends z.ZodTypeAny>(
path: string,
schema: TZodType,
): Promise<z.infer<TZodType>> {
let rawConfig: Record<string, TomlPrimitive>;
try {
rawConfig = parse(await Bun.file(path).text());
} catch (err: unknown) {
if (!(err instanceof Error)) {
throw err;
}
throw new Error(`error parsing config "${path}": ${err.message}`);
}
try {
// There isn't any way to fix this afaik.
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return schema.parse(rawConfig) as z.infer<TZodType>;
} catch (err: unknown) {
if (!(err instanceof ZodError)) {
throw err;
}
throw fromError(err);
}
} TS definitely sees the return type as |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to make a generic 'parse response' function in a database module designed for interacting with DynamoDB:
I'm trying to follow the implementation in the docs for using generic type arguments for schemas. However, even with the suggested typecasting for
return validItem.data as z.infer<T>;
, TypeScript and ESLint are still not happy with that particular line:ESLint: Unsafe return of type 'any' from function with return type 'TypeOf<T> | null'.(@typescript-eslint/no-unsafe-return
Can someone help me out with what I'm doing wrong here?
Beta Was this translation helpful? Give feedback.
All reactions