忽略 TypeScript 类型错误

TypeScript type errors are ignored

Here is SystemJS + TypeScript plunk, created from 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/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
  .catch(console.error.bind(console));
</script>
...

通常我更喜欢 TypeScript 停止应用程序执行并抛出类型错误。

这个设置有什么问题?为什么抑制 TypeScript 错误?如何更改此设置?

您可以使用 plugin-typescript for SystemJS, which, as of version 6, has the option to enable typechecking in the browser (note that this option is removed in version 7).

但是你必须完全按照其 readme 中的说明来配置该插件,也就是说,在 SystemJS 配置中使用 mainmeta 之类的 typescript 包这个:

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

然后,因为这个main,你必须把map里面的typescript改成

'typescript': 'npm:typescript@2.2.1'

然后,你还需要将这些添加到typescriptOptions:

experimentalDecorators: true,
typeCheck: true

进行这些更改后,控制台中会打印类型错误:

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

但我认为没有办法在类型错误时停止程序执行 - 据我所知,noEmitOnErrors 对 plugin-typescript 没有影响。

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