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

--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.
主站蜘蛛池模板: 肥城市| 龙里县| 永善县| 汕尾市| 宽城| 安西县| 铁力市| 东兰县| 仲巴县| 米泉市| 阿合奇县| 塘沽区| 林甸县| 夏邑县| 潞西市| 罗城| 罗城| 阳江市| 濉溪县| 连江县| 嘉祥县| 岳普湖县| 东乡县| 永济市| 岳普湖县| 临沧市| 乐陵市| 金坛市| 成安县| 麻栗坡县| 南陵县| 彭山县| 华池县| 浦城县| 濮阳市| 集贤县| 高陵县| 济源市| 望都县| 乐平市| 当雄县|