wvbe / slimdom-sax-parser

Parse an XML string to a light-weight spec-compliant document object model, for browser and Node
https://www.npmjs.com/package/slimdom-sax-parser
MIT License
8 stars 5 forks source link

Cannot access slimdom types in TypeScript #25

Open rrthomas opened 1 year ago

rrthomas commented 1 year ago

In TypeScript 4.9.3, with slimdox-sax-parser 1.4.1 and slimdom 2.3.2, if I try this MWE:

import assert from 'assert'
import slimdom from 'slimdom'
import {sync as parseXML} from 'slimdom-sax-parser'

let elem: slimdom.Element
let doc
try {
    doc = parseXML('some text')
} catch (error) {
    throw new Error('error parsing doc')
}
assert(doc.documentElement !== null)
elem = doc.documentElement

it builds fine.

If I use slimdom-sax-parser >= 1.5.0, with any version of slimdom >= 2.3.2, then I get an error like this:

src/foo.ts:13:1 - error TS2322: Type 'import("/home/rrt/Software/ruth/node_modules/slimdom-sax-parser/node_modules/slimdom/dist/slimdom").Element' is not assignable to type 'import("/home/rrt/Software/ruth/node_modules/slimdom/dist/slimdom").Element'.

13 elem = doc.documentElement
   ~~~~

I tried fixing slimdom to the 3.0.1, the same version as used by slimdom-sax-parser 1.5.3, but I get the same error.

If on the other hand I import slimdom from slimdom-sax-parser, as shown in the slimdom-sax-parser docs, so that the second two import statements in the code above are replaced with the single statement:

import {slimdom, sync as parseXML} from 'slimdom-sax-parser'

then I get this error instead:

src/bar.ts:4:11 - error TS2503: Cannot find namespace 'slimdom'.

4 let elem: slimdom.Element
            ~~~~~~~

So, I cannot work out how I am supposed to access the slimdom types from TypeScript with slimdom-sax-parser >= 1.5.0.

Thanks for slimdom-sax-parser!

wvbe commented 1 year ago

Hey @rrthomas, thanks for reporting! I've in the past struggled with the idea of namespaces a bit so it's quite likely I made an error. I'll look into this soon as I can, but maybe you want to unblock yourself sooner than that.

I'll update this ticket again when I make any progress. Thanks again for reporting!

rrthomas commented 1 year ago

Thanks for your suggestions. Unfortunately I want to parse fragments with the additionalNamespaces option, so slimdom's parseXmlDocument doesn't work for me, but it was a good suggestion. I don't use position tracking at all, so I am happy to stick with 1.4.1 for now.

wvbe commented 1 year ago

I've not made any progress on this yet, and having become a dad recently I don't anticipate that I'll get to it pretty soon either. Unfortunately this issue will need to remain unsolved for a while, unless somebody else picks it up :) Sorry!

rrthomas commented 7 months ago

I've managed to use slimdom's (new?) parseFragment method now. But I guess this bug should remain open.