Conversation
| errors: undefined, | ||
| }); | ||
| return success(values[name]); | ||
| return success((values as unknown as V)[name][index][subfieldName] as Values[K]); |
There was a problem hiding this comment.
as Values[K] is needed because the function expects it to be that.
function validateSubfield<
SK extends keyof SubFormValues<Values> & string,
K extends SubFormKeys<Values>,
V extends ArrayRecord<Values, SK>
>(
name: K,
index: number,
subfieldName: SK,
values: Values
): Promise<Result<NonEmptyArray<FieldError>, Values[K]>>The return type should be something like "Promise<Result<NonEmptyArray<FieldError>, ArrayItem<Values[K]>[SK]>>" as that's what we're actually returning.
There was a problem hiding this comment.
ah uhm, I see what you mean, Values[K] does not seem right (still it is being used and casted to in a lot of other places)
giogonzo
left a comment
There was a problem hiding this comment.
Thanks @pedrodim!
The added tests look good to me, as well as the shady cast: there might be a better way to rewrite the function overall and remove the need for casting, but adding one more (same as others already used in the same function) seems ok for now
|
I'll take a better look tomorrow as I agree with you there is something weird going on with the subform validation typings 😅 |
All the tests pass, it works now even if you validate only part of the subForm or none at all.
Having said that, I still not sure the fix is safe, the cast seems weird to me... or maybe is the return type of
validateSubfieldthat's wrong 😄