NaturalIntelligence / NaturalIntelligence/fast-xml-parser
Enhance type definition for XMLParser `isArray` option when using TypeScript
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 3.1k
- Forks
- 395
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 1
Description
- Are you running the latest version?
- Have you included sample input, output, error, and expected output?
- Have you checked if you are using correct configuration?
- Did you try online tool?
- Have you checked the docs for helpful APIs and examples?
Description
When creating an instance of XMLParser using the isArray option, the method arguments are currently defined as the following, with the jPathOrMatcher argument being either a string or Matcher type:
The behaviour as documented is that if the jPath option is set to true (or undefined since the default is true), then the type of jPathOrMatcher will be a string, and if jPath is false then the type of jPathOrMatcher will be a Matcher.
However, since the type of jPathOrMatcher is currently string | Matcher, to satisfy TypeScript I either have to add unnecessary conditional logic to check whether the type is string or Matcher, or add in a type assertion like (jPathOrMatcher as string).includes("blah").
I would like to propose updating the types so that it can smartly detect whether the jPath option is true or false, and set the type of isArray based on that. I've found that this can be achieved using union types, and I have a minimal working example using the TypeScript Playground:
Would you like to work on this issue?
- Yes
- No
Bookmark this repository for further updates. Visit SoloThought to know about recent features.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the XMLParser type definition for the isArray option and compare it with the linked TypeScript Playground example. Update the types so jPath true or undefined selects string and jPath false selects Matcher, then verify that TypeScript users no longer need conditional checks or assertions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100