timohausmann / quadtree-ts

Quadtree Typescript Implementation
https://timohausmann.github.io/quadtree-ts/
MIT License
131 stars 15 forks source link

Unable to import? #6

Closed frnsys closed 1 year ago

frnsys commented 2 years ago

Thank you for this library. I'm having a strange issue where the imports are undefined after I import them.

I'm just doing:

import { Quadtree, Rectangle } from '@timohausmann/quadtree-ts';

and the TypeScript compiler is fine with it. However, when I actually load the page I get the following error:

_timohausmann_quadtree_ts__WEBPACK_IMPORTED_MODULE_2__.Quadtree is not a constructor

and Quadtree is undefined.

If I import the default export:

import Quadtree from '@timohausmann/quadtree-ts';

Quadtree is no longer undefined and looks like the correct Quadtree class. However TypeScript then complains and won't compile since I guess the type definition doesn't declare it as a class.

Bjvanminnen commented 2 years ago

I ran into the same issue. It looks to me as if the types declaration is wrong - it claims that Quadtree is a named export, whereas it is actually the default export. I managed to hack around this by editing node_modules/@timohausmann/quadtree-ts/types/index.esm.d.ts to properly reflect this

i.e., replace the first two lines with:

import { Quadtree } from './Quadtree';
export default Quadtree;
timohausmann commented 2 years ago

Thanks for the info, I will try to find the time to test and fix this. This entire umd/cjs/esm bundling is so weird.

rafael84 commented 1 year ago

Hey there. I recently migrated my project to Typescript and noticed the same issue. Any luck on finding the root cause?

Blo0dR0gue commented 1 year ago

You need to change "browser": "dist/quadtree.umd.full.js", to "browser": "dist/quadtree.esm.js" in the package.json of this lib.

timohausmann commented 1 year ago

The idea is to use named exports only. I could recreate the issue with Webpack 5.

I found this "export" property for webpack and added it to package.json: https://webpack.js.org/guides/package-exports/#providing-different-versions-depending-on-target-environment

I tested again and it seems to work now.

npm install -S @timohausmann/quadtree-ts@latest

Please let me know if the issue persists.

yar2001 commented 1 year ago

The latest version is able to work!

timohausmann commented 1 year ago

@yar2001 awesome, thanks for your feedback!

etienne-85 commented 10 months ago

Hi, I'm still having issue mentionned above: MapGen.tsx:83 Uncaught TypeError: (0 , _quadtreeTs.Quadtree) is not a constructor I'm using version @timohausmann/quadtree-ts@2.2.2 with parcel builder. Any idea?

Note that if I use default import as someone suggested before, it seems to fix my issue.. import Quadtree from "@timohausmann/quadtree-ts";