1

I'm trying TypeScript especially to work with classes but I encounter an issue after my TS file is compiled in JS.

Here is the TS code for my class (PartenaireTSModel.ts):

export namespace Partenaires {
    export class Partenaire {
        private _IdPartenaire : number;
        private _DateCreation : Date;   

        public get IdPartenaire(){ return this._IdPartenaire; }
        public set IdPartenaire(idPartenaire: number){ this._IdPartenaire = idPartenaire; }

        public get DateCreation(){ return this._DateCreation; }
        public set DateCreation(dateCreation: Date){ this._DateCreation = dateCreation; }
    }
} 

My TS code (Partenaires.ts):

import { Partenaires } from "./Models/PartenaireTSModel"

let partenaire = new Partenaires.Partenaire();
partenaire.IdPartenaire = 12;

After compiling the result in JS (Partenaires.js):

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var PartenaireTSModel_1 = require("./Models/PartenaireTSModel");
var partenaire = new PartenaireTSModel_1.Partenaires.Partenaire();
partenaire.IdPartenaire = 12;

I have this error when I launch my app : Uncaught ReferenceError: exports is not defined. (on line 2 of Partenaires.js)

Also tried :

import p = require("./Models/PartenaireTSModel");
let partenaire = new p.Partenaires.Partenaire(); 

instead of this :

import { Partenaires } from "./Models/PartenaireTSModel"
let partenaire = new Partenaires.Partenaire();

But I get the same error.

Here is my tsconfig.js :

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "outDir": "wwwroot/js"
  },
  "include": [
    "TSScripts/**/*"
  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Can someone help me ?

Thank you,

Philippe

1

1 Answer 1

1

Add "module": "es6", in the compilerOptions object of tsconfig.json file. (So, the generated modules code runs natively in the browsers)

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

Comments

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.