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

--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.
主站蜘蛛池模板: 洛南县| 赤城县| 宁武县| 读书| 阳新县| 繁昌县| 林周县| 旬邑县| 郎溪县| 石嘴山市| 淳安县| 白玉县| 景洪市| 论坛| 安溪县| 保康县| 青州市| 富蕴县| 长兴县| 祁阳县| 江油市| 比如县| 嵊泗县| 如东县| 塔河县| 宜兰县| 大荔县| 荔浦县| 松滋市| 开鲁县| 馆陶县| 西和县| 宁津县| 高阳县| 肥城市| 虹口区| 乌鲁木齐市| 安徽省| 雷波县| 迁安市| 凌云县|