Here is SystemJS + TypeScript plunk, được tạo từ official Angular plunk template..ts tệp không được coi là mô-đun TypeScript
Nó ném
(SystemJS) SyntaxError: Missing initializer in const declaration
at eval()
...
lỗi và rõ ràng là đánh giá nộp .ts JavaScript như bình thường khi tập tin không chứa import
hoặc export
báo cáo:
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>
...
Nhưng the same plunk is fine khi tập tin có dấu hiệu ES6 mô-đun:
main.ts
const foo: boolean = 'foo';
console.log(foo);
export default null;
Rõ ràng, nếu một tệp có phần mở rộng .ts, tôi muốn đánh giá nó như TypeScript, cho dù nó có nhập gì đó hay không.
Tại sao điều này xảy ra trong thiết lập này? Làm thế nào để sửa cái này?
Bạn nên sử dụng 'plugin-typescript'. 'systemjs' không đi kèm với transpiler vì vậy chỉ cần' transpiler: 'typescript'' không hoạt động – unional
Tôi không nghĩ đó là sự thật. TS hoạt động nếu có câu lệnh 'export'. Đây là những gì câu hỏi nói. Nếu không 'const foo: boolean = 'foo'' sẽ ném một lỗi cú pháp, phải không? – estus