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

Type annotations

Types for JavaScript variables are determined at runtime. Types for JavaScript variables can also change at runtime. For example, a variable that holds a number can later be replaced by a string. Usually, this is unwanted behavior and can result in a bug in our app.

TypeScript annotations let us declare variables with specific types when we are writing our code. This allows the TypeScript compiler to check that the code adheres to these types before the code executes at runtime. In short, type annotations allow TypeScript to catch bugs where our code is using the wrong type much earlier than we would if we were writing our code in JavaScript.

TypeScript annotations let us declare variables with types using the :Type syntax.

  1. Let's browse to the TypeScript playground and enter the following variable declaration into the left-hand pane:
let unitPrice: number;
  1. The transpiled JavaScript will appear on the right-hand side as follows:
var unitPrice;
That let has been converted to var. This is because the compiler that the playground uses is set to target a wide range of browsers, some of which don't support let . Also, notice that the type annotation has disappeared. This is because type annotations don't exist in JavaScript.
  1. Let's add a second line to our program:
unitPrice = "Table";

Notice that a red line appears under unitPrice, and if you hover over it, you are correctly informed that there is a type error:

  1. You can also add type annotations to function parameters for the return value using the same :Type syntax. Let's enter the following function into the playground:
function getTotal(unitPrice: number, quantity: number, discount: number): number {
const priceWithoutDiscount = unitPrice * quantity;
const discountAmount = priceWithoutDiscount * discount;
return priceWithoutDiscount - discountAmount;
}

We've declared unitPrice, quantity, and discount parameters, all as numbers. The return type annotation comes after the function's parentheses, which is also a number in the preceding example.

We have used both const and let to declare variables in different examples. let will allow the variable to change the value after the declaration, whereas const variables can't change. In the preceding function, priceWithoutDiscount and discountAmount never change the value after the initial assignment, so we have used const.
  1. Let's call our function with an incorrect type for quantity and assign the result to a variable with an incorrect type:
let total: string = getTotal(500, "one", 0.1);

We find that one is underlined in red, highlighting that there is a type error:

  1. If we then correct one to 1, total should be underlined in red, highlighting that there is a type problem with that:

The TypeScript compiler uses type annotations to check whether values assigned to variables and function parameters are valid for their type.

This strong type checking is something that we don't get in JavaScript, and it is very useful in large code bases because it helps us immediately detect type errors.

主站蜘蛛池模板: 克什克腾旗| 呈贡县| 塔城市| 高邮市| 荥经县| 岳池县| 延安市| 辰溪县| 新闻| 南涧| 星子县| 鸡泽县| 双鸭山市| 宣恩县| 黎平县| 合川市| 吉安市| 铁力市| 长治市| 广汉市| 旬阳县| 临泽县| 石首市| 承德市| 全椒县| 冷水江市| 容城县| 黔西| 莒南县| 文水县| 廉江市| 尼玛县| 甘德县| 铁力市| 阜宁县| 敦化市| 嘉禾县| 洪湖市| 宣化县| 东乡| 全南县|