Hello i'm trying to create an interface to get the following return in typescript:
{
"field": "departament_name",
"errors": [
"constraint": "O nome do departamento precisa ser do tipo String",
] },
but I'm not getting it I'm getting the following error when I try to add the object to my array:
Type 'string []' is not assignable to type 'constraints []'. Type 'string' is not assignable to type 'constraints'.ts (2322)
interface:
export interface constraints {
constraint: string
}
export interface errorFormater {
field: string;
errors: constraints[]
}
function:
export const formatErrors = (validationErrors: ValidationError[]): errorFormater[] => {
let response: errorFormater[] = [];
for (let error of validationErrors) {
let field: string = error.property;
let constraints: string[] = [];
for (let constraint in error.constraints) {
if (!error.constraints.hasOwnProperty(constraint)) {
continue;
}
constraints.push(error.constraints[constraint]);
};
// console.log(property, errorMessage)
response.push({ field, errors: constraints });
}
return response;
}
let constraints: string[]
not the same aserrors: constraints[]
when you try pushresponse.push({ field, errors: constraints });
– Nikita Madeeverrors
is an array of object, should contain braces {} else,:
is not allowed in array of strings. is your sample data correct? – Vaibhav