Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Module Loading Work with TypeScript

Tags:

typescript

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?

like image 366
EisenbergEffect Avatar asked Oct 01 '12 21:10

EisenbergEffect


2 Answers

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.

like image 91
chuckj Avatar answered Oct 15 '22 04:10

chuckj


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"
    ]
}
like image 41
dade Avatar answered Oct 15 '22 04:10

dade