Illustrative example of the concept I am attempting to implement:
const simpleExample = <T>(v: T = 'cat') => {
return v;
};
Issue encountered:
Error message: Type 'string' is not assignable to type 'T'.
'T' could be instantiated with a different type that may not relate to 'string'.ts(2322)
The inference process seems to function properly when default parameters are not used. Perhaps wrapping it in a construct that handles defaulting may solve this issue... It's perplexing why it fails in this scenario...
My specific implementation:
I aim for each Shape
, Z
, and Output
to be deduced from the arguments provided, which works without issues if the default argument is excluded...
const createHooks = <
Shape extends z.ZodRawShape,
Z extends z.ZodObject<Shape, 'strict'>,
Output extends z.ZodTypeAny
>(
schema: Z,
effect: (s: Z) => z.ZodEffects<Z, Output> = (s: Z) => s.transform((i) => i)
) => <Source, FProps extends { [k: string]: Record<string, any> }>({
name,
getInstanceKey,
load,
fieldProps,
}: {
name: string;
getInstanceKey: (s: Source) => string;
load: (s: Source) => Z['_input'];
fieldProps: FProps;
}) => {
// ...
return { }
}
Raised error:
'Output' could potentially have an unrelated type to '{ [k in keyof addQuestionMarks<{ [k in keyof Shape]: Shape[k]["_output"]; }>]: addQuestionMarks<{ [k in keyof Shape]: Shape[k]["_output"]; }>[k]; }'
This situation is frustrating as I expect any arbitrary type to trigger an error.