I've written a simple validation method for a form that takes a { [name: string]: string } object of values in and returns a partial of the same type with error messages, but something about my types isn't working- the Third E typed argument is erroring unless I call it any with either:
Type '{}' is not assignable to type 'E'. if I give no type information (noas any).Cannot find name 'E'if I preface= {}with the:EtypeType expectedif I useas E
export const requiredFields: <
T extends { [propName: string]: string },
K extends keyof T,
E extends Partial<T>
>(
fields: K[],
values: T,
startingErrors?: E
) => E = (fields, values, startingErrors: E = {}) =>
reduce(
fields,
(acc, fieldName) =>
values[fieldName].length
? acc
: Object.assign({}, acc, { [fieldName]: "Required" }),
startingErrors
)