Closed sarahdayan closed 10 months ago
Can you please explain me your changes for parse function? What exactly we're expecting
@amitguptagwl This is only a type change. parse
used to return any
, forcing you to cast it. Now you can provide a generic type to maintain type-safety, which is usually preferrable as many linters frown upon type casting.
What I can understand here is to define the return type of parser to ExpectedObject
with properties title
and description
which is not true for parser. Hence, I'm confusd what exactly you mean with the type you defined.
In the example they gave the type ExpectedObject
has the properties title
and description
but in a real world use that type have the properties that you expect the parsed XML to have.
For example for this XML:
<person>
<name>John</name>
<surname>Doe</surname>
</person>
the corresponding Typescript type would be:
type Person = {
name: string;
surname: string;
}
and you would use it like:
const result = xml.parse<Person>(data);
This would make the result
variable have the type Person
instead of any
which is much better in terms of type safety.
You can read more about generics here.
Purpose / Goal
This PR improves typing for
build
andparse
by making both functions generic.It also fixes the return type of
build
, which should bestring
.parse
build
Type
Please mention the type of PR