Here is SystemJS + TypeScript plunk,从official Angular plunk template创建。.ts文件不被视为TypeScript模块
它抛出
(SystemJS) SyntaxError: Missing initializer in const declaration
at eval()
...
错误和明显评估的.ts文件作为普通的JavaScript时,文件不包含import
或export
声明:
main.ts
const foo: boolean = 'foo';
console.log(foo);
config.js
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
...
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
...
}
});
的index.html
...
<script src="https://unpkg.com/[email protected]/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
...
但the same plunk is fine当文件具有ES6模块的迹象:
main.ts
const foo: boolean = 'foo';
console.log(foo);
export default null;
显然,如果一个文件有.ts扩展名,I宁愿将其评估为TypeScript,无论它是否导入。
为什么在此设置中会发生这种情况?这怎么解决?
您应该使用'plugin-typescript'。 'systemjs'不附带译码器,所以'transpiler:'typescript''不起作用 – unional
我不认为这是真的。如果出现'export'语句,TS将起作用。这就是问题所在。否则'const foo:boolean ='foo''会抛出一个语法错误,不是吗? – estus