anvaka / ngraph.graph

Graph data structure in JavaScript
BSD 3-Clause "New" or "Revised" License
520 stars 66 forks source link

Support for ECMA Script import #50

Closed tomas-javurek closed 1 year ago

tomas-javurek commented 1 year ago

Hi, first of all, ngraph is a great project!

Maybe I'm overworked but trying to import ngraph.graph in my TypeScript project I'm not successfull:

import createGraph from 'ngraph.graph' cause TypeError: createGraph.default is not a function import { createGraph } from 'ngraph.graph' cause Module '"ngraph.graph"' has no exported member 'createGraph'.

Am I doing something wrong or I missed something? Couldn't find any solution on the web :(

anvaka commented 1 year ago

Do you mind sharing a base project configuration?

tomas-javurek commented 1 year ago

I'm trying to use something like const graph = createGraph(), but it caused errors above mentioned. I found in a module index.d.ts the default function export default function createGraph... so I guess it should work well, but it doesn't.

anvaka commented 1 year ago

Those errors are coming from the tooling that you use. Likely you have a typescript compiler and probably webpack/rollup/etc. Each has their own configuration - can you share those configs please?

tomas-javurek commented 1 year ago

This is my tsconfig.json file

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@common/*":["src/common/*"],
      "@decorators/*":["src/decorators/*"],
      "@admin/*":["src/admin/*"],
      "@modules/*":["src/modules/*"],
      "@guards/*":["src/guards/*"]
    }
  }
}
tomas-javurek commented 1 year ago

I found a solution. Adding "esModuleInterop": true in tsconfig.json do the trick. See https://www.typescriptlang.org/tsconfig#esModuleInterop

anvaka commented 1 year ago

glad you found it!

tomas-javurek commented 1 year ago

It would be maybe very nice to support your project by defining types on npm @types/ngraph.graph. Other libraries, such ngraph.from/todot would be typed as well... It's very nice and usefull project and going accross the development in TypeScript can spread its use I guess.