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

--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.
主站蜘蛛池模板: 平度市| 平谷区| 宜君县| 广水市| 远安县| 衡南县| 太仓市| 华池县| 廉江市| 河津市| 香港| 皋兰县| 闽侯县| 得荣县| 大庆市| 陆良县| 江源县| 鹤山市| 泾阳县| 缙云县| 乐业县| 广平县| 古田县| 东莞市| 南投县| 霍州市| 太湖县| 巨鹿县| 读书| 阿拉善右旗| 新安县| 梧州市| 商洛市| 库尔勒市| 磴口县| 九台市| 安福县| 屏南县| 平顺县| 紫云| 垣曲县|