I have multiple types acceptable as a value in an object and one of them is array. When I get an array as parameter, I want to override it as a comma separated list. Apparently, after making sure the element I am trying to convert is indeed an array, Typescript returns a type related error:
for (const key in parameters) {
if (Array.isArray(parameters[key])) {
parameters[key] = parameters[key].join(',');
} ^^^^
TS2339: Property 'join' does not exist on type
parameters
type definition looks like this: parameters: Record<string, string | boolean | number | Array<string>
How can I get it to realize that the parameter is indeed an array in this context?
CodePudding user response:
Use another variable:
let parameters : Record<string, string | boolean | number | Array<string>> =
{
's' : 1
}
for (const key in parameters) {
let ass = parameters[key];
if (Array.isArray(ass)) {
parameters[key] = ass.join(',');
}
}