Skip to content

Conversation

@tmcw
Copy link
Collaborator

@tmcw tmcw commented Feb 15, 2025

This resolves two fairly long-standing issues. It does two things:

  1. Removes @types/geojson and @xmldom/xmldom as peerDependencies. If you want to use them and their types with TypeScript, you install those packages separately.
  2. Adds a union type for all Document parameters that go into the module to signal compatibility with xmldom.

@tmcw
Copy link
Collaborator Author

tmcw commented Feb 15, 2025

This doesn't work, if xmldom is not available, then this lets the methods accept any.

@tmcw
Copy link
Collaborator Author

tmcw commented Feb 15, 2025

If anyone knows the answer here, let me know: is it possible to expose an index.d.ts file that tries to use types from an optional dependency, but if that module is not present, treats its types as never?

@tmcw
Copy link
Collaborator Author

tmcw commented Mar 7, 2025

Okay, I think this is roughly the path as-is. There's no optional-type-if-you-have-it-installed construct afaict, so if you use this module + typescript, you'll need xmldom/xmldom and types/geojson.

Testcase for both kinds of issues:

import { kml } from "@tmcw/togeojson";
import { DOMParser as XDOMParser } from "@xmldom/xmldom";

const x = new DOMParser();

const d = await fetch("hi")
	.then((r) => r.text())
	.then((r) => x.parseFromString(r, "text/xml"));

const x2 = new XDOMParser();

const d2 = await fetch("hi")
	.then((r) => r.text())
	.then((r) => x2.parseFromString(r, "text/xml"));

const y = kml(d);
// this should have an accurate features type
y.features;
const y2 = kml(d2);
// This should error
kml(1);

@tmcw tmcw merged commit 3d58707 into main Mar 7, 2025
1 check passed
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.

Incompatible type with xmldoc Why is @types/geojson a required dependency?

2 participants