btree-typescript icon indicating copy to clipboard operation
btree-typescript copied to clipboard

Uncaught TypeError: BTree is not a constructor

Open invegat opened this issue 2 years ago • 7 comments

const tree = new BTree()

get error Uncaught TypeError: BTree is not a constructor

invegat avatar Jun 16 '22 01:06 invegat

How are you importing it? import BTree from 'sorted-btree'; should work. But if not, try this:

import BTree_ from 'sorted-btree';
const BTree = (BTree_ as any).default as typeof BTree_;

Let me know how it goes.

qwertie avatar Jun 16 '22 01:06 qwertie

image

Though I was able to import with your code.

paolodina avatar Dec 28 '22 00:12 paolodina

I hit the same problem with bun.sh.

Here a repo with reproduction:

  • https://github.com/mindreframer/btree-constructor-issue

mindreframer avatar Jun 03 '23 11:06 mindreframer

Please try the code above and let me know how it goes.

qwertie avatar Jun 04 '23 03:06 qwertie

@qwertie Thanks for the response! This works:

import BTree_ from "sorted-btree";
const BTree = (BTree_ as any).default as typeof BTree_;

let tree = new BTree();

I have this issue with a 3rd party lib (https://github.com/blinkdb-js/blinkdb), would it be possible to make it work in the expected way? Is this workaround necessary to keep the ES5 compatibility?

And if there is no other way, maybe this should be documented in the Readme.

Cheers and have a great Sunday! Roman

mindreframer avatar Jun 04 '23 09:06 mindreframer

The workaround is problematic in that sense, that it would be not compatible with npm or yarn anymore. That means if we consider a library for publishing, to be used either in Node.js or Bun.sh projects, this approach would not work.

mindreframer avatar Jun 04 '23 09:06 mindreframer

Unfortunately I don't understand the cause of the problem, and I am generally too busy to investigate, so it is unlikely that I will find time to figure out the problem. It appears that enabling esnext in TypeScript options is problematic? Maybe if BTree were recompiled in esnext mode, it would work properly, but we can't do that, because we need to maintain es5 compatibility.

qwertie avatar Jun 04 '23 16:06 qwertie