Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript error? #14

Closed
BraunTom opened this issue Sep 25, 2018 · 6 comments
Closed

Typescript error? #14

BraunTom opened this issue Sep 25, 2018 · 6 comments

Comments

@BraunTom
Copy link

Maybe it is because I am inexperienced with Typescript, but if you import something in Typescript it tries to load the default property of the exported object, which gives me the error:

new flatten_js_1.default.Point(x, y);
TypeError: Cannot read property 'Point' of undefined

because in the index.js is only module.exports = f (if I change it to
module.exports.default = f;
it fixes the problem for me)

@SamuelQZQ
Copy link

I have the same problem.

import Flatten from 'flatten-js';

When I import like this in Typescript, Flatten is undefined.

In the index.js, if I change module.exports = f to module.exports.default = f fixes the problem for me.

But I think that's not the best solution.

@OldrichDlouhy
Copy link
Contributor

OldrichDlouhy commented Jan 23, 2019

Use the following syntax to import flatten in Typescript:

import * as flatten from "flatten-js";

@SamuelQZQ
Copy link

My problem has been solved. I think it's nothing about this library, but my own fault. Maybe this issue can be closed?

@alexbol99
Copy link
Owner

I am also not so experienced with TypeScript but the last suggestion seems correct for now. Later I am planning to redesign the library to es6 modules, then TypeScript support will be more straightforward.

@alexbol99
Copy link
Owner

I've just found that moving to es6 module system (import/export) make things even less straightforward.
In this discussion is recommended to add following setup to compilerOptions in tsconfig.json file

"compilerOptions": {
   ...
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },

@SamuelQZQ , can you check if this works in your case?

@SamuelQZQ
Copy link

I import like this :

import Flatten from 'flatten-js';

If I do not use "esModuleInterop": true in tsconfig.json, Flatten will be undefined. So, your setup works for me.

My full compilerOptions

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es2017",
    "jsx": "react",
    "allowJs": true,
    "downlevelIteration": true,
    "esModuleInterop": true,
    "lib": [
      "es6",
      "es2017",
      "dom"
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants