Question

In TypeScript, if I am targeting a browser, how does module loading work? Can I use require.js to load modules? does it have it's own loader?

Was it helpful?

Solution

TypeScript does not provide a runtime. You need to supply a module loader to use, such as requirejs. A TypeScript module can either be generated to CommonJS convention (for use with node.js) or AMD convention (as used in requirejs); which it generates is a compiler switch.

OTHER TIPS

As Chuckj mentioned, TypeScript does not provide a runtime. You need to supply a module loader to use.

What you then need to do is to tell the TypeScript compiler to generate the JS to confirm with the module loader that would be used at runtime.

You can do this by specifying the module loader to the compiler using -m compiler flag:

tsc -m commonjs //'amd', 'system', 'umd' or 'es2015'

or by specifying the module in the compilerOptions in your tsconfig.json file:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "module": "commonjs" //'amd', 'system', 'umd' or 'es2015'
    },
    "exclude": [
        "node_modules"
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top