简体   繁体   English

如何让 Typescript 抛出运行时错误?

[英]How to make Typescript throw runtime error?

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
 constructor() {
   console.log(this.getColor(1000))
}
getColor(c:String) {
  return c
}
}

My text editor put a red line bellow the 1000 that's says: Argument of type '1000' is not assignable to parameter of type 'String'.ts(2345)我的文本编辑器在 1000 下方放了一条红线,上面写着:“1000”类型的参数不可分配给“字符串”类型的参数。ts(2345)

Ok...but my app still executes and I can have the result on my console.好的...但是我的应用程序仍在执行,我可以在控制台上看到结果。

is there any how to make Angular and/or Typescript to prevent an execution in a scenario like this?有什么方法可以使 Angular 和/或 Typescript 在这种情况下防止执行?

Your browser does not know about TypeScript, it only executes the transpiled javascript code.您的浏览器不知道 TypeScript,它只执行转译的 javascript 代码。

But in your situation, the build process (made by Angular) will raise errors and you won't be able to deploy your application.但是在您的情况下,构建过程(由 Angular 制作)会引发错误,您将无法部署您的应用程序。

In dev mode a typescript error is raised but you still can execute the application for practical reasons在开发模式下,会出现 typescript 错误,但出于实际原因您仍然可以执行该应用程序

Typescript is only warning you, that something is not right. Typescript 只是警告您,有些事情是不对的。 It does still compile and you can still run the code and it can work, but it is not intended to work like that.它仍然可以编译,您仍然可以运行代码并且它可以工作,但它并不打算那样工作。

So how can you check if the input is correct?那么如何检查输入是否正确呢?

function example (color: string): void {
  if (typeof color !== 'string') {
    throw Error() // you can put your error message in here
  }
}

You can configure your compiler for --noEmitOnError :您可以为--noEmitOnError 配置编译器

Do not emit outputs if any errors were reported.如果报告了任何错误,请不要发出输出。

Though this won't "prevent an execution" per se, it will avoid Typescript's deliberate tendency to allow you to emit even when it detects type errors.尽管这本身不会“阻止执行”,但它会避免 Typescript 故意允许您在检测到类型错误时发出的倾向。 From TypeScript development lead RyanCavanaugh in Typescript issue 828 , where that command-line flag was created:来自 TypeScript 开发负责人 RyanCavanaughTypescript 问题 828中,其中创建了该命令行标志:

We definitely want the behavior that the compiler can emit in the presence of type errors.我们绝对想要编译器在出现类型错误时可以发出的行为。 This is a key scenario for migrating existing JavaScript -- you rename some.js file to.ts, get some type errors, but want to keep getting compilation of it while you refactor it to remove the type errors.这是迁移现有 JavaScript 的关键场景——您将 some.js 文件重命名为 .ts,得到一些类型错误,但希望在重构它以消除类型错误时继续编译它。 They are 'warnings' in that sense;从这个意义上说,它们是“警告”; we cannot guarantee that your program does not work just because it has type errors.我们不能保证您的程序不能仅仅因为它有类型错误而工作。

That said, we recognize that this isn't always the behavior you want.也就是说,我们认识到这并不总是您想要的行为。 Incremental builds get messed up by this on a fairly regular basis.增量构建经常会因此而混乱。 We need something to address this scenario.我们需要一些东西来解决这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM