I recently studied and made simple “hello worlds” with TypeScript. There is something that I think I can’t wrap my head around, and here's how to use System.js with TypeScript. Every tutorial or demo there on the Internet is about Angular2, and I don't want to participate in Angular 2.
As an example, I have the following project structure:
RootFolder | | _lib | ...... ts (where .ts files are) | | components (where compiled .js files are) | libraries | ......... systemjs (where system.js is) | | index.html | tsconfig.json
My tsconfig.json file looks like this:
{ "compileOnSave": true, "compilerOptions": { "noImplicitAny": true, "noEmitOnError": true, "removeComments": false, "sourceMap": true, "target": "es5", "module": "system", "moduleResolution": "node", "outDir": "./components" }, "exclude": [ "node_modules", "wwwroot" ], "include": [ "./_lib/ts/**/*" ] }
TypeScript compilation works as expected, and there is no problem with this. I created a simple class called "Alerter" containing the following code:
//alerter.ts class Alerter { showMessage(): void { alert("Message displayed."); } } export default Alerter
And app.ts (which is my "main" application file) with the following code:
//app.ts import Alerter from "./alerter"; console.log("app.js included & executed"); function test() { console.log("test called"); const alt = new Alerter(); alt.showMessage(); };
And in my index.html, I just want to import this .js application into System.js and just want to call the "test" function from the console. But that does not work. No matter what I did, I just can't access this feature. I see that the first console.log is running, but when I try to call test () from the Chrome console, it is undefined.
If I remove the "alerter" class dependency from my main.ts, everything will work. Since compiled app.js contain only console.log calls and function definitions.
Here are my calls to System.js in index.html
System.config({ packages: { "components": { defaultExtension: "js" } } }); System.import("components/app");
I am really desperate now and think I should just go back to the days of jQuery. It is so simple but cannot make it work.