没有d.ts文件

时间:2016-03-21 10:36:45

标签: typescript

如果我没有与我使用的外部库相对应的打字(在TypeScript文件中安装和引用),我希望我的TypeScript应用程序的编译失败。目前,没有它们,编译就会成功。

以下是我tsconfig.json的内容:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "noEmitOnError": false,
    "declaration": true
  }
}

以下是我想抛出错误的代码(toastr变量未定义且仅在运行时提供):

import {Component} from 'angular2/core';

@Component({
  selector: 'my-app',
  template: `
    <div>Hello world</div>
  `
})
export class AppComponent {
  constructor() {
    toastr.info('I am here for few seconds');
  }
}

谢谢!

2 个答案:

答案 0 :(得分:2)

使编译器闭合的一种快捷方法是将 toastr 变量声明为任何变量。

declare toastr: any;

答案 1 :(得分:2)

默认情况下,即使出现错误,TypeScript也会发出JavaScript。 This is a feature. See Why TypeScript

那说你有tsconfig "noEmitOnError": false,。如果将其更改为true,则在出现任何错误时不会发出。请注意,这会降低增量编译性能,因为编译器需要执行整个程序分析,甚至在单个文件上发出一个emit。