P
P
photosho2020-10-11 08:24:10
typescript
photosho, 2020-10-11 08:24:10

How to compile a TypeScript project correctly?

There is the following code in TypeScript:

class A {
    a: string;
    b: number;

    constructor() {
      this.a = 'str1';
      this.b = 20;
    }
  }

  class B extends A {
    c: string;

    constructor() {
      super();

      this.c = 'str2';
    }
  }

  var a: B = new B;

  console.log(a);


I can run the compilation either directly through "tsc", or with the help of a collector, everything works fine. The next step is to separate the classes by writing them in different modules.

a.ts module:

export class A {
    a: string;
    b: number;

    constructor() {
      this.a = 'str1';
      this.b = 20;
    }
  }


index.ts module:

import {A} from './a';

  class B extends A {
    c: string;

    constructor() {
      super();

      this.c = 'str2';
    }
  }

  var a: B = new B;

  console.log(a);


This is where the problems begin. Compiling "tsc" in the browser gives an error:

exports is not defined


When using the collector - if I want to compile the result into one file, then I put the "module": "system" parameter in "tsconfig.json" - the following error appears:

System is not defined


If I put "module": "amd" - the error is this:

define is not defined


If I set "module": "commonjs", then it no longer accepts the "outFile" parameter, it compiles all ts-files separately. When connecting these files to the project, the browser gives the following error:

exports is not defined


Prompt the correct sequence of actions. It is advisable to set up the assembly of the project in one js file so as not to include each module in html separately.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question