feat(types): improve isArray option type inference#804
Open
abhu85 wants to merge 1 commit intoNaturalIntelligence:masterfrom
Open
feat(types): improve isArray option type inference#804abhu85 wants to merge 1 commit intoNaturalIntelligence:masterfrom
abhu85 wants to merge 1 commit intoNaturalIntelligence:masterfrom
Conversation
Add discriminated union types for X2jOptions based on the jPath option. When jPath is true (default), callbacks like isArray, tagValueProcessor, attributeValueProcessor, etc. receive jPath as a string. When jPath is false, callbacks receive a Matcher instance. This allows TypeScript to properly narrow the type of the callback parameter based on the jPath option value, eliminating the need for type assertions when using string methods on jPath. Exports new types: - X2jOptionsBase: Base options shared by both modes - X2jOptionsWithJPathString: Options for jPath: true (string callbacks) - X2jOptionsWithMatcher: Options for jPath: false (Matcher callbacks) Fixes NaturalIntelligence#803
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves the TypeScript type definitions for the
isArrayoption and other callbacks that receivejPathorMatcherparameters, addressing the concerns raised in #803.The Problem
The current type definition uses
string | Matcher(whereMatcher = unknown) for callbacks likeisArray,tagValueProcessor, etc. This forces users to use type assertions even when they knowjPath: true(the default):The Solution
This PR introduces discriminated union types based on the
jPathoption:New Exported Types
X2jOptionsBase: Base options shared by both jPath modesX2jOptionsWithJPathString: Options whenjPath: true(callbacks receivestring)X2jOptionsWithMatcher: Options whenjPath: false(callbacks receiveMatcher)X2jOptions: Union of the above two typesAffected Callbacks
All callbacks that previously used
string | Matchernow properly narrow based onjPath:isArraytagValueProcessorattributeValueProcessorupdateTagignoreAttributes(function form)Test Plan
Fixes #803