TypeScript: How to import a JavaScript ES6 class?

I have a JavaScript ES6 class named DB defined like this:

// db.js
"use strict";

export default class DB {
  ...
}

      

And a TypeScript file that looks like this:

// surgeons.ts
"use strict";

import DB from "./db";

      

I expect this to just work, but I am getting the following error:

Browserify Error { [TypeScript error: app/source/common/surgeons.ts(3,16): Error TS2307: Cannot find module './db'.]
  message: 'app/source/common/surgeons.ts(3,16): Error TS2307: Cannot find module \'./db\'.',
  fileName: 'app/source/common/surgeons.ts',
  line: 3,
  column: 16,
  name: 'TypeScript error' }

      

I tried to define the db.d.ts file as described here , but this generated an error:

Browserify Error { [TypeScript error: app/source/common/surgeons.ts(5,16): Error TS2306: File 'app/source/common/db.d.ts' is not a module.]
  message: 'app/source/common/surgeons.ts(5,16): Error TS2306: File \'app/source/common/db.d.ts\' is not a module.',
  fileName: 'app/source/common/surgeons.ts',
  line: 5,
  column: 16,
  name: 'TypeScript error' }

      

My tsconfig looks like this:

{
  "compilerOptions": {
    "module": "es6",
    "target": "es6",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true
  },
  "include": [
    "app/source/**/*",
    "test/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

      

+3


source to share


1 answer


Per Daniel Rosenwasser, the installation "allowJS": true

in the section tsconfig.json

"compilerOptions"

did the trick. I didn't need to use the file .d.ts

at all.



+3


source







All Articles