admin管理员组

文章数量:1335544

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 piled JavaScript:

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

As you see in the piled 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!

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 piled JavaScript:

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

As you see in the piled 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!

Share Improve this question asked Aug 18, 2017 at 8:33 JulianGJulianG 1,5811 gold badge19 silver badges29 bronze badges 1
  • possible duplicate: stackoverflow./questions/40463441/… – DDRamone Commented Aug 18, 2017 at 8:40
Add a ment  | 

2 Answers 2

Reset to default 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.

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 {
  ...
}

本文标签: nodejsUse JavaScript class in TypeScriptStack Overflow