Fraction.js icon indicating copy to clipboard operation
Fraction.js copied to clipboard

TypeScript: TypeError: default is not a constructor errors

Open jayaddison opened this issue 2 years ago • 6 comments

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"]}

jayaddison avatar Oct 20 '23 14:10 jayaddison

This happened to me after updating typescript to ^4.9.5.

frostburn avatar Nov 15 '23 16:11 frostburn

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 avatar Nov 15 '23 17:11 jayaddison

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 avatar Nov 15 '23 17:11 jayaddison

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 avatar Nov 15 '23 17:11 jayaddison

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.

jayaddison avatar Nov 15 '23 17:11 jayaddison

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"

ShaneCallananAerlytix avatar Jan 29 '24 15:01 ShaneCallananAerlytix