2017-04-14 148 views
1

Here is SystemJS + TypeScript plunk,從official Angular plunk template創建。忽略TypeScript類型錯誤

,因爲它表明,它忽略所有打字稿類型的錯誤,已記錄到控制檯的唯一事情就是foo

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> 
... 

一般來說,我寧願使用TypeScript來停止應用程序的執行並拋出類型錯誤。

這個設置有什麼問題?爲什麼要抑制TypeScript錯誤?這個設置如何改變?

+0

設置沒有問題,在瀏覽器中進行類型檢查並不容易,因爲某人必須以某種方式在瀏覽器中提供編譯器所需的所有類型定義。爲了避開這個問題,SystemJS使用'transpileModule()'打字稿方法,它不會按照設計進行類型檢查。對於0.19,你可以使用[plugin-typescript](https://github.com/frankwallis/plugin-typescript)並將它的'typeCheck'選項設置爲true,但是從SystemJS 0.20開始[這不再可能](https ://github.com/frankwallis/plugin-typescript/issues/194)。我切換到webpack。 – artem

+0

@artem謝謝,很高興知道。 0.20是一個顯示限制器有幾個原因,但可能[仍有希望](https://github.com/frankwallis/plugin-typescript/issues/185#issuecomment-275999466),我不確定。你知道如何更新plunk來使用plugin-typescript嗎? [我結束了](https://plnkr.co/edit/xNOvL4efra2IGEUaQxpe?'TypeError:AMD模塊https://unpkg.com/[email protected]/lib/typescript.js沒有定義錯誤。 – estus

回答

1

對於SystemJS,您可以使用plugin-typescript,該版本在版本6中可以選擇在瀏覽器中啓用類型檢查(請注意this option is removed in version 7)。

但是你必須精確地confiure該插件在其readme拼寫出來,那就是,有typescript包SystemJS配置與mainmeta這樣的:

typescript: { 
    "main": "lib/typescript.js", 
    "meta": { 
    "lib/typescript.js": { 
     "exports": "ts" 
    } 
    } 
} 

然後,因爲這main,你必須在map

'typescript': 'npm:[email protected]' 

改變typescript然後,你還需要這些添加到typescriptOptions

experimentalDecorators: true, 
typeCheck: true 

這些更改後,正在被打印在控制檯類型錯誤:

unpkg.com/[email protected]/lib/plugin.js:498 TypeScript [Error] 
Type '"foo"' is not assignable to type 'boolean'. (TS2322) 

但我不認爲有一種方式來對類型錯誤停止執行程序 - noEmitOnErrors沒有影響據我所知,插件打字稿。

我保存了更新的plunk here,不知道它會保留多久。

+0

我很欣賞這一點。奇蹟般有效。如果你願意刪除它,我已經將工作分配給了https://plnkr.co/edit/hzHAddtsCJZAZkMRRtHS?p=preview。 – estus