sorry for the confusing title,i'm not sure how to explain my question clearly. currently, i'm try to implement a method which could parse a object from a xml. the xml (use xml-js to translate to js) looks like this
interface OpmlElement {
attributes: {
text:string
},
elements:OpmlElement[]
}
the target object look like this
interface ParsedTestCase {
title?: string;
suites?: string[];
}
so, i want to declare some kind of parser to parse this xml. idefine some kind of parser
const elementParserTable= [
{
check: (e:OpmlElement) => getText(e).startsWith("tt:"),
takeValue: (e:OpmlElement) => getText(e),
cb: (v:string)=> {
testcase.title=v
}
} ,
{
check: (e:OpmlElement) => getText(e).startsWith("ts:"),
takeValue: (e:OpmlElement) =>getText(e).split(","),
cb: (v:string[])=> {
testcase.suites=v
}
},
]
the first question is when i use like above
const elements:OpmlElement[]=[]
for (const e of elements) {
for (const elementParser of elementParserTable) {
if (elementParser.check(e)) {
elementParser.cb(elementParser.takeValue(e));
continue;
}
}
}
Argument of type 'string | string[]' is not assignable to parameter of type 'string & string[]'. Type 'string' is not assignable to type 'string & string[]'. Type 'string' is not assignable to type 'string[]'.
how could i avoid this? and the second question is: is there some way to constrain the elementParser in elementParserTable to make sure that the return type of takeValue function is the cb function's parameter type?