2017-04-14 150 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