官术网_书友最值得收藏!

--noImplicitReturns

This ensures we return a value in all branches of a function if the return type isn't void.

Let's see this in action with an example:

  1. In our OrderDetail class, let's say we have the following implementation for our getTotal method:
getTotal(discount: number): number {
if (discount) {
const priceWithoutDiscount = this.product.unitPrice * this.quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
} else {
// We forgot about this branch!
}
}
  1. We've forgotten to implement the branch of code that deals with the case where there is no discount. If we compile the code without the --noImplicitReturns flag, it compiles fine:
tsc orderDetail
  1. However, let's see what happens if we compile the code with the --noImplicitReturns flag:
tsc orderDetail --noImplicitReturns

We get the following error, as expected:

orderDetail.ts(9,31): error TS7030: Not all code paths return a value.
主站蜘蛛池模板: 东丰县| 会东县| 临邑县| 施甸县| 铜山县| 阳信县| 平潭县| 陇川县| 滨海县| 开江县| 张家界市| 南部县| 西吉县| 富川| 安溪县| 镇赉县| 青铜峡市| 平罗县| 诸暨市| 和林格尔县| 开江县| 华坪县| 临澧县| 宁远县| 东辽县| 利津县| 中阳县| 城市| 南靖县| 郁南县| 湘西| 上林县| 黔西县| 随州市| 大化| 西乌珠穆沁旗| 历史| 富平县| 信宜市| 安岳县| 库尔勒市|