Simple example of what I am trying to do:
const simpleExample = <T>(v: T = 'cat') => {
return v;
};
Error:
Type 'string' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.ts(2322)
The inference will work if I don't try to have a default parameter, so maybe the options is to wrap it in something that does the defaulting.. I don't understand why this doesn't work..
My actual code:
I want each of these Shape, Z, and Output to be infered from arguments and it works if I don't supply the default arg...
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 { }
}
Gets the error:
'Output' could be instantiated with an arbitrary type which could be unrelated to '{ [k in keyof addQuestionMarks<{ [k in keyof Shape]: Shape[k]["_output"]; }>]: addQuestionMarks<{ [k in keyof Shape]: Shape[k]["_output"]; }>[k]; }'
Which is frustrating, because I want any 'arbitrary type' to cause an error.