parse5 icon indicating copy to clipboard operation
parse5 copied to clipboard

Node types don't appear to be exported in an ergonomic way

Open justinfagnani opened this issue 5 months ago • 4 comments

I've been trying to figure out how to import the node types (Node, Document, etc) and can't figure out any way to do it except via the DefaultTreeAdapter type and an index expression, which is quite non-obvious and tedious:

import {parse, type DefaultTreeAdapterMap} from 'parse5';

type Document = DefaultTreeAdapterMap['document'];
type Node = DefaultTreeAdapterMap['node'];

The API docs mention the DefaultTreeAdapterTypes namespace, but while that's in the source in index.ts:

export type * as DefaultTreeAdapterTypes from './tree-adapters/default.js';

That line doesn't appear in my installed dist/index.d.ts at least in version 7.1.2.

I can't import the type's exported from parse5/dist/tree-adapters/default.js because that file isn't in the package exports.

justinfagnani avatar Jan 11 '24 20:01 justinfagnani

FYI the @parse5/tools package does re-export the default tree adapter types at least (by indexing into it like you mentioned)

Possibly could use that to make it a bit easier

Would be good to know why the export doesn't show up still

43081j avatar Jan 11 '24 20:01 43081j

The individual node types don't show up because they just aren't exported in any public module. Is there a reason that parse5/tree-adapters/default.js isn't exported?

justinfagnani avatar Jan 12 '24 02:01 justinfagnani

if i remember correctly, it was mostly because there are multiple tree adapters. so it didn't seem to make much sense exporting the default one's types (since everything else in the package is generic)

i'm not sure though tbh. i ran into the same so i just added it to the @parse5/tools package in the end (import {Element} from '@parse5/tools')

43081j avatar Jan 12 '24 08:01 43081j

Well, the other tree adapters should export their types too :)

justinfagnani avatar Jan 12 '24 18:01 justinfagnani