I am VERY green with regard to TypeScript (so be kind), but I have been unable to figure out how to stop TypeScript from adding what appears to be "magic" code to the resulting Javascript after transpiling.
Whenever I use "tsc", no errors are reported, but Typescript appends the following as the last line in the Javasccript file:
export {};
... and when the Javascript file is executed (loaded in a browser) this results in the following error being reported by the console:
Uncaught SyntaxError: Unexpected token 'export'
The compiler options in tsconfig.json are:
"compilerOptions": {
"target": "ES2015",
"removeComments": false,
"noImplicitAny": true,
"noImplicitThis": true,
"moduleResolution": "Node"
},
I then try changing the compiler options to the following:
"compilerOptions": {
"target": "ES2015",
"module":"CommonJS",
"removeComments": false,
"noImplicitAny": true,
"noImplicitThis": true,
"moduleResolution": "Node"
},
Success - the export line is no longer present, but now, the following line is added to the top of the javascript file!:
Object.defineProperty(exports, "__esModule", { value: true });
... and (again) when the Javascript file is executed (loaded in a browser) this results in the following error being reported by the console:
Uncaught ReferenceError: exports is not defined
I also tried to change the script tag that loads the javascript produced by tsc to make use of type="module", i.e.:
<script type="module" src="file_produced_by_tsc.js"></script>
... when the page is loaded locally, the console displays:
Access to script at 'file:///...' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
Access to script at 'file:///...' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
Failed to load resource: net::ERR_FAILED
Obviously, the browser is telling me the script cannot be loaded locally, but I typically do my JS debugging that way. It would be a royal pain to have to upload the script just to load it from a network resource!
Good grief -- all I want is the Javascript code WITHOUT any "magic code" added. A compiler option such as:
"excludeMagicCode": true
... would be ideal!
Obviously, I am missing something very basic; is there a compiler option that can prevent these magic code statements from littering the resulting Javascript?
Thanks in advance.