8

I have a problem introducing TypeScript to our JavaScript project. First I want to use TypeScript only in my part of the code, leaving the JavaScript untouched.

Now I try to use a JavaScript class in my TypeScript code, but I don't find a solution in the last days.

The head of my TypeScript class with import of the JavaScript:

import { BaseLogic } from "../baseLogic";
export class ClaimLogic extends BaseLogic {
...

The JavaScript class ("baseLogic.js"):

module.exports = class BaseLogic {
    constructor(meta, logger) {
    ...

My *.d.ts file ("baseLogic.d.ts"):

export class BaseLogic {
    meta: any;
    log: any;

    constructor(meta: any, logger: any)
}

The head of the compiled JavaScript:

const baseLogic_1 = require("../baseLogic");
class ClaimLogic extends baseLogic_1.BaseLogic {
...

As you see in the compiled JavaScript baseLogic_1.BaseLogic is used. This results in following error:

TypeError: Class extends value undefined is not a constructor or null

With only baseLogic_1 after the extends keyword in the JavaScript file all is fine.

I have no idea about a solution and hope you can help me!

1

2 Answers 2

3

Your import suppose to be import * as BaseLogic from "../baseLogic";.

In that way you will get the Class that you put on module.exports.

3
  • With this import I musst use "extends BaseLogic.BaseLogic" in the TypeScript code. If not, I got the error "is not a constructor or function type"
    – JulianG
    Commented Aug 18, 2017 at 9:14
  • No, you will get the Class, so you should extend BaseLogic
    – felixmosh
    Commented Aug 18, 2017 at 10:06
  • Sadly the TypeScript compiler is of another opinion
    – JulianG
    Commented Aug 18, 2017 at 10:13
1

The codesnipet in baseLogic.js exports the class.

module.exports = class BaseLogic {
  constructor(meta, logger) {
  ...
}

You try to access with class ClaimLogic extends baseLogic_1.BaseLogic an object that includes the class BaseLogic

Solution

import BaseLogic from '../baseLogic'
// or:  const BaseLogic = require("../baseLogic");

class ClaimLogic extends BaseLogic {
  ...
}
8
  • Thank you for your answer, but with your solution I got following error: "../baseLogic has no default export"
    – JulianG
    Commented Aug 18, 2017 at 9:12
  • can you try it again with import {BaseLogic} from '../baseLogic'
    – Roman
    Commented Aug 18, 2017 at 9:14
  • Now the error in the TypeScript file is gone, but it's lead to the same compiled JS file.
    – JulianG
    Commented Aug 18, 2017 at 9:16
  • I think there is a problem with the *.d.ts file, but I don't now
    – JulianG
    Commented Aug 18, 2017 at 9:17
  • do you try to import the java- oder typescript file?
    – Roman
    Commented Aug 18, 2017 at 9:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.