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: TypeError: default is not a constructor errors #72

Open
jayaddison opened this issue Oct 20, 2023 · 6 comments
Open

TypeScript: TypeError: default is not a constructor errors #72

jayaddison opened this issue Oct 20, 2023 · 6 comments

Comments

@jayaddison
Copy link

This is an error I encounter on v4.3.5 but not in v4.3.3 and earlier.

The TypeScript code I'm using is fairly similar to the README example.

import * as assert from 'assert';
import { describe, it } from 'mocha';

import Fraction from 'fraction.js';


describe('repro case', function() {

  it('constructs fraction', function() {
    const result: string = new Fraction('1.23').simplify(0.1).toFraction(true);
    assert.equal('1.2', result);
  });

});

The error output when this test runs is:

  1) repro case
       constructs fraction:
     TypeError: fraction_js_1.default is not a constructor
      at Context.<anonymous> (src/app/repro.spec.ts:10:28)
      at processImmediate (node:internal/timers:471:21)

The contents of my local tsconfig.json file are: {"include": ["src/**/*.ts"]}

@frostburn
Copy link

This happened to me after updating typescript to ^4.9.5.

@jayaddison
Copy link
Author

Ok, hm. This was found using TypeScript 5.2.2 and running a unit test using mocha.

Trying to remove mocha from the equation seems to help: the following code runs with no problems when run using ts-node:

import Fraction from 'fraction.js';

const result: string = new Fraction('1.23').simplify(0.1).toFraction(true);
console.log(result);

Output:

$ ts-node foo.ts
1 1/4

(as expected)

That seems like it could rule out fraction.js as the source of the problem, at least in the mocha test case.

@jayaddison
Copy link
Author

Ah, blast. That output was produced using fraction.js v4.2.1 - potentially unaffected, as per the issue description. I'll check again with v4.3.5 in a few moments.

@jayaddison
Copy link
Author

Ok, yep: fraction.js v4.3.5 is indeed borken with the previously-mentioned minimal code snippet.

import Fraction from 'fraction.js';

const result: string = Fraction('1.23').simplify(0.1).toFraction(true);
console.log(result);
$ ts-node foo.ts
oo.ts:3:24 - error TS2348: Value of type 'typeof Fraction' is not callable. Did you mean to include 'new'?

3 const result: string = Fraction('1.23').simplify(0.1).toFraction(true);

I'll try reducing the TypeScript version next, following up on @frostburn's comment.

@jayaddison
Copy link
Author

No luck as far back as TypeScript v4.3.5 here - the minimal snippet (without any of the mocha module references) still seems to fail with v4.3.5 of Fraction.js - version coincidence coincidental..

In the previous comment I'd omitted the new keyword; restoring that produces the same initially-reported error message:

const result: string = new Fraction('1.23').simplify(0.1).toFraction(true);
                       ^
TypeError: fraction_js_1.default is not a constructor

I'm being a bit spammy here, so I'll pause for a while.

@ShaneCallananAerlytix
Copy link

I'm encountering the same issue with jest, no matter which version of fraction.js I use.
My Typescript version is 5.1.5

In my tsconfig, I've specified:

  • esModuleInterop: true
  • allowSyntheticDefaultImports: true
  • module: "esnext"

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

No branches or pull requests

3 participants