最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typescript extend class with imported class - Stack Overflow

programmeradmin7浏览0评论

If I extend a locally exported class, it works.

Working example:

export class classA {

    constructor() {
        super();
    }
}

export class classB extends classA {

constructor() {
    super();

    this.do();
}

private do(): void {
    // do something
}

But when I import classA from outside of the file it's not work.

not Working example:

import { classA } from '../'; // I use index.ts file, so the reference is good.

export class classB extends classA {
    constructor() {
        super();

        this.do();
    }

    private do(): void {
        // do something
    }
}

File structure:

Root/
- index.ts

- classA(folder)/
   - index.ts
   - classA.ts

- classB(folder)/
   - index.ts
   - classB.ts

The index.ts file inside classA folder:

export * from './classA';

The root index.ts file:

export * from './classB';
export * from './classA';

Error message: class classB extends _1.classA TypeError: Class extends value undefined is not a constructor or null

I need to load outside from this file, because I would like to use classA in other files... Any idea why happen this?

[Solved]: In the root index.ts file the order have to be:

export * from './classA';
export * from './classB';

If I extend a locally exported class, it works.

Working example:

export class classA {

    constructor() {
        super();
    }
}

export class classB extends classA {

constructor() {
    super();

    this.do();
}

private do(): void {
    // do something
}

But when I import classA from outside of the file it's not work.

not Working example:

import { classA } from '../'; // I use index.ts file, so the reference is good.

export class classB extends classA {
    constructor() {
        super();

        this.do();
    }

    private do(): void {
        // do something
    }
}

File structure:

Root/
- index.ts

- classA(folder)/
   - index.ts
   - classA.ts

- classB(folder)/
   - index.ts
   - classB.ts

The index.ts file inside classA folder:

export * from './classA';

The root index.ts file:

export * from './classB';
export * from './classA';

Error message: class classB extends _1.classA TypeError: Class extends value undefined is not a constructor or null

I need to load outside from this file, because I would like to use classA in other files... Any idea why happen this?

[Solved]: In the root index.ts file the order have to be:

export * from './classA';
export * from './classB';
Share Improve this question edited Jun 1, 2017 at 16:22 Alfabravo 7,5896 gold badges49 silver badges83 bronze badges asked May 16, 2017 at 17:50 TwoisTwois 5981 gold badge7 silver badges19 bronze badges 8
  • Can you post the code for ../index.ts? Are you exporting class A as default? – rossipedia Commented May 16, 2017 at 18:01
  • @rossipedia I updated the question with the file structure and the content of index.ts files – Twois Commented May 16, 2017 at 18:18
  • Why not reference classA directly in classB? import { classA } from '../classA/classA'; – Poku Commented May 16, 2017 at 18:36
  • There are many folder in root, and it is simpler than write down the direct reference in every file. - But I take a try, may be this help me. – Twois Commented May 16, 2017 at 18:40
  • 1 Do not put a solution in the question. Post it as an answer. – user663031 Commented May 16, 2017 at 19:03
 |  Show 3 more ments

2 Answers 2

Reset to default 2

Import classA directly. Try this:

import { classA } from '../classA/classA'; 
export class classB extends classA {

constructor() {
    super();

    this.do();
}

private do(): void {
    // do something
}

In the root index.ts file the order have to be:

export * from './classA';
export * from './classB';
发布评论

评论列表(0)

  1. 暂无评论