-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Address issues with typings #123
base: master
Are you sure you want to change the base?
Conversation
@cjbarth @lukeggchapman Please review and provide feedback. |
I don't think this is entirely correct. These typing changes result in |
The only situation where So in that situation,
So there is no real inconsistency, though it seems that the definition of |
If you look at This means that |
In the XPath 1.0 data model, an XPath expression can only evaluate to one of four types, and barring any ill-defined custom functions, any given expression will always evaluate to the same type:
The function you are referring to contains a series of
Accessing the
Yes, my modification in this PR accounts for this.
The XPath 1.0 data model has no concept of a |
Thank you for that thorough explanation. It seems that |
Made the change. Thanks. |
export function isNodeLike(value: SelectSingleReturnType): value is Node; | ||
export function isArrayOfNodes(value: SelectReturnType): value is Node[]; | ||
export function isElement(value: SelectSingleReturnType): value is Element; | ||
export function isAttribute(value: SelectSingleReturnType): value is Attr; | ||
export function isTextNode(value: SelectSingleReturnType): value is Text; | ||
export function isCDATASection(value: SelectSingleReturnType): value is CDATASection; | ||
export function isProcessingInstruction(value: SelectSingleReturnType): value is ProcessingInstruction; | ||
export function isComment(value: SelectSingleReturnType): value is Comment; | ||
export function isDocumentNode(value: SelectSingleReturnType): value is Document; | ||
export function isDocumentTypeNode(value: SelectSingleReturnType): value is DocumentType; | ||
export function isDocumentFragment(value: SelectSingleReturnType): value is DocumentFragment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In testing this in xml-crypto
it seems that it would be helpful to take in null
and undefined
as arguments, this way I don't have to have multiple checks.
export function isNodeLike(value: SelectSingleReturnType): value is Node; | |
export function isArrayOfNodes(value: SelectReturnType): value is Node[]; | |
export function isElement(value: SelectSingleReturnType): value is Element; | |
export function isAttribute(value: SelectSingleReturnType): value is Attr; | |
export function isTextNode(value: SelectSingleReturnType): value is Text; | |
export function isCDATASection(value: SelectSingleReturnType): value is CDATASection; | |
export function isProcessingInstruction(value: SelectSingleReturnType): value is ProcessingInstruction; | |
export function isComment(value: SelectSingleReturnType): value is Comment; | |
export function isDocumentNode(value: SelectSingleReturnType): value is Document; | |
export function isDocumentTypeNode(value: SelectSingleReturnType): value is DocumentType; | |
export function isDocumentFragment(value: SelectSingleReturnType): value is DocumentFragment; | |
export function isNodeLike(value?: SelectSingleReturnType | null): value is Node; | |
export function isArrayOfNodes(value?: SelectReturnType | null): value is Node[]; | |
export function isElement(value?: SelectSingleReturnType | null): value is Element; | |
export function isAttribute(value?: SelectSingleReturnType | null): value is Attr; | |
export function isTextNode(value?: SelectSingleReturnType | null): value is Text; | |
export function isCDATASection(value?: SelectSingleReturnType | null): value is CDATASection; | |
export function isProcessingInstruction(value?: SelectSingleReturnType | null): value is ProcessingInstruction; | |
export function isComment(value?: SelectSingleReturnType | null): value is Comment; | |
export function isDocumentNode(value?: SelectSingleReturnType | null): value is Document; | |
export function isDocumentTypeNode(value?: SelectSingleReturnType | null): value is DocumentType; | |
export function isDocumentFragment(value?: SelectSingleReturnType | null): value is DocumentFragment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The definition of SelectSingleReturnType
already includes undefined
, so it doesn't seem like there's a need to re-indicate that in the function signatures. SelectReturnType
doesn't include undefined
, but I'm not sure I can see a good case for having isArrayOfNodes
accept undefined
.
And ?
is for indicating a parameter that can be omitted completely, so that wouldn't be appropriate here.
I think I can see the value of having these function accept null
, but in that case, we should have a type for that to ensure uniformity and reduce duplication.
type SelectSingleReturnTypeOrNull = SelectSingleReturnType | null;
type SelectReturnTypeOrNull = SelectReturnType | null;
export function isNodeLike(value: SelectSingleReturnTypeOrNull): value is Node;
export function isArrayOfNodes(value: SelectReturnTypeOrNull): value is Node[];
export function isElement(value: SelectSingleReturnTypeOrNull): value is Element;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you prefer a new type of something like type Nullable<T> = T | null
with Nullable<SelectReturnType>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that would be good. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it occurred to me that we can probably just take any
and narrow from there.
I also wonder if we should more consistently name these checking function: e.g. should |
Yeah, I suppose it makes sense to have them all end with |
See #125 |
It appears from #127 that people are using these new support functions. I've created #128 to continue to validate these functions as I work on the next release of |
Note that TS gives a compile error even if you are not using the function, just because it's trying to typecheck all the types of the library (I'm in that case) |
I don't have a meaningful opinion for this specific use case but in my experience, it's often interesting to make typeguard slightly more clever / complex so that their input can be "unknown" (which is always the case if you are doing JS anyway), at the price of performance of course, as validation becomes more costly. |
@forty , I can certainly make a change in my other branch to accommodate that suggestion. In fact, it does make sense to use |
I've moved all this type checking code to another repo so that we don't muddy the purpose of I'll still gladly do what it takes to close out this PR or otherwise help it along as @JLRishe directs. |
This addresses some issues with the recent type changes:
select()
functions return a Node array, string, number, or boolean, but as of now, the .d.ts has them returning a Node array, Node, string, boolean, or nullselect1()
call, so I have updated them to reflect that. Likewise, theisNodeArray()
function makes sense when called on the result of aselect()
call.