Skip to content

Conversation

tpluscode
Copy link
Contributor

It can be useful to use equals to narrow down a term's type. For example, in my code I have

if (functionPtr.term.equals(dashSparql.in) || functionPtr.term.equals(dashSparql.notin)) {
  return new InExpression(pointer.term, functionPtr.term, expressionList)
}

export class InExpression extends FunctionExpression {
  constructor(exprTerm: Term, funcTerm: typeof dashSparql.in | typeof dashSparql.notin, args: NodeExpression[]) {
}

See that funcTerm requires Named Node with one of very specific URIs. Right now, even though the type is ensured by the if statement, the type of functionPtr.term is a "plain" NamedNode, causing a TS error.

By adding this is T return type assertion to equals, this type is narrowed down in the body of the condition.

Copy link

changeset-bot bot commented Apr 11, 2025

🦋 Changeset detected

Latest commit: 6206428

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@rdfjs/types Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants