feat: TermWrapper.from static factory with intersection return type#83
Draft
jeswr wants to merge 1 commit into
Draft
feat: TermWrapper.from static factory with intersection return type#83jeswr wants to merge 1 commit into
jeswr wants to merge 1 commit into
Conversation
Adds an overloaded public static from(term, dataset, factory) to TermWrapper that is runtime-equivalent to the constructor but returns the intersection of the (sub)class instance type and the RDF/JS term type of the wrapped term (NamedNode when a string IRI is passed, the inferred term type otherwise). Instances created this way can be passed directly to factory.quad(), dataset.match() and any other RDF/JS Term position without casts. Fully non-breaking: the constructor and the existing typed getters are untouched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jul 6, 2026
Closed
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.
Adds an overloaded
public static from(term, dataset, factory)factory toTermWrapper.What it does
from(term: string, dataset, factory)returnsInstanceType<This> & NamedNode<string>from(term: T extends Term, dataset, factory)returnsInstanceType<This> & Treturn new this(term, dataset, factory)— identical runtime behaviour to the constructor, so subclasses get the factory for free (Child.from(...)returnsChild & NamedNode).Because the return type intersects the wrapper with the RDF/JS term type, instances created via
fromcan be passed directly tofactory.quad(...),dataset.match(...)and any otherTermposition without casts — the pain point both #61 and #74 set out to fix.What it deliberately does not do
This is the minimal, fully non-breaking slice: no generic parameter on
TermWrapper, no getter restructuring, noIRdfJsTermremoval, no changes to the#subjectPredicatemapper signatures. The constructor and all existing typed getters are untouched, so existing code compiles unchanged.Relation to existing PRs
Supersedes the
TermWrapper.fromslice of #74 (itsfromoverloads and README updates are adopted here verbatim) and the#fromslice of #61. The remaining parts of those PRs (#in, class-level generics, getter/IRdfJsTermrestructuring, mapper signature changes) are intentionally left for follow-ups.Testing
test/unit/term_wrapper_from.test.ts: runtime assertions (subclass instance creation, constructor equivalence for string andTerminputs, accessor/dataset/factory behaviour) plus compile-level assertions (result assignable toNamedNode/Quad_Subject; passed toDataFactory.quadandDatasetCore.matchwith no casts;Literalterm type preserved).npm test: 132 tests, 0 fail (5 pre-existing skips); new code at 100% coverage.npx typedoc: 0 errors; the 6 warnings are all pre-existing onmain.