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

The opt-out type checking - any

The any type is very useful when we do not know what to expect from a function (in other words, when we do not know which type we are going to return):

  1. Create a new file called any.ts inside the chapter-02 folder, and add the following code:
let band: any;
band = {
name: "Motorhead",
description: "Heavy metal band",
rate: 10
}
console.log(band);
band = "Motorhead";
console.log(band);

Note that the first band assignment is an object, while the second is a string.

  1. Go back to your Terminal and compile and run this piece of code; type the following command:
tsc any.ts
  1. Now, let's see the output. Type the following command:
node any.js

You will see the following message in the Terminal:

{ name: 'Motorhead', description: 'Heavy metal band', rate: 10 }
Motorhead

Here, we can assign anything to our band variable.

主站蜘蛛池模板: 铁岭县| 宾川县| 博客| 册亨县| 海伦市| 陇西县| 南乐县| 克什克腾旗| 云和县| 锡林浩特市| 定州市| 岑溪市| 大石桥市| 中山市| 陕西省| 大洼县| 侯马市| 双牌县| 阿瓦提县| 土默特左旗| 马关县| 新竹县| 霍林郭勒市| 宜丰县| 合水县| 调兵山市| 新干县| 茂名市| 勐海县| 石泉县| 新绛县| 温泉县| 义乌市| 梧州市| 鹤峰县| 金乡县| 泰顺县| 齐齐哈尔市| 中牟县| 华坪县| 怀柔区|