When given the type,
export type ValidationErrors<T extends string> = Partial<Record<T, string>> & { errorsCount: number };
You have the ability to create an object in this manner:
const errors: ValidationErrors<'hello' | 'goodbye'> = {
errorsCount: 0,
hello: 'Hello',
}
But when ValidationErrors
is used with a generic parameter, you might encounter the error
Type '{ errorsCount: number; }' is not assignable to type 'ValidationErrors<T>'.
. How can this issue be resolved?
Here's how it looks in action:
const doSomething = <T extends string>() => {
// Type '{ errorsCount: number; }' is not assignable to type 'ValidationErrors<T>'.
const errors: ValidationErrors<T> = {
errorsCount: 0,
}
return errors
}
const abc = doSomething<'hello'>()
abc.errorsCount // will return a number
abc.hello // will return a string or undefined