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.

Sign up to request clarification or add additional context in comments.

3 Comments

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"
No, you will get the Class, so you should extend BaseLogic
Sadly the TypeScript compiler is of another opinion
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 Comments

Thank you for your answer, but with your solution I got following error: "../baseLogic has no default export"
can you try it again with import {BaseLogic} from '../baseLogic'
Now the error in the TypeScript file is gone, but it's lead to the same compiled JS file.
I think there is a problem with the *.d.ts file, but I don't now
do you try to import the java- oder typescript file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.