linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
566 stars 146 forks source link

Type Problems #379

Closed thatcort closed 4 years ago

thatcort commented 4 years ago

I'm having some type compatibility problems using the latest version 1.0.7-2. This could be because I'm very inexperienced with the library, so any advice is appreciated.

DataFactory.namedNode:

import { IndexedFormula, NamedNode, DataFactory } from 'rdflib'; const nn: NamedNode = DataFactory.namedNode('https://example.com/foo'); <--- ERROR

Type 'NamedNode' is missing the following properties from type 'NamedNode': classOrder, dir, site, doc, and 9 more.ts(2740)

It seems that DataFactory.namedNode returns an instance of an interface NamedNode defined in tf-types.d.ts, while NamedNode imported from rdflib is a class defined in named-node.d.ts. Which is correct? This seems to be the same problem as the next issue.

DataFactory.triple:

    const s = DataFactory.namedNode(subjectIRI);
    const p = DataFactory.namedNode(predicateIRI);
    const o = DataFactory.namedNode(objectIRI);
    const t = DataFactory.triple(s, p, o); <--- ERROR

Argument of type 'NamedNode' is not assignable to parameter of type 'SubjectType'. Type 'import("...../node_modules/rdflib/lib/tf-types").NamedNode' is not assignable to type 'import("...../node_modules/rdflib/lib/named-node").default'.ts(2345)

IndexedFormula.remove:

    const g = DataFactory.namedNode(IndexedFormula.defaultGraphURI);
    const q = DataFactory.quad(s, p, o, g);
    const store = new IndexedFormula();
    store.remove(q); <---- ERROR

Argument of type 'Statement' is not assignable to parameter of type 'Quad<Quad_Subject, Quad_Predicate, Quad_Object, Quad_Graph> | Quad<Quad_Subject, Quad_Predicate, Quad_Object, Quad_Graph>[]'. Type 'Statement' is not assignable to type 'Quad<Quad_Subject, Quad_Predicate, Quad_Object, Quad_Graph>'. Types of property 'object' are incompatible. Type 'ObjectType' is not assignable to type 'Quad_Object'. Type 'Collection<Node | BlankNode | Literal | Collection | Variable>' is not assignable to type 'Quad_Object'. Type 'Collection<Node | BlankNode | Literal | Collection | Variable>' is missing the following properties from type 'Literal': language, datatypets(2345)

megoth commented 4 years ago

This is related to https://github.com/linkeddata/rdflib.js/issues/376

@joepio is looking into it ^_^

joepio commented 4 years ago

You're using it correctly, the problem has to do with internal type inconsistencies that slipped through. Thanks for the detailed report. I've added tests for your use cases and fixed (loosened up) the types.

PR with fix is open: https://github.com/linkeddata/rdflib.js/pull/380