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

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.

主站蜘蛛池模板: 高唐县| 沙田区| 西乌| 开阳县| 红桥区| 新巴尔虎左旗| 遵义市| 偃师市| 博白县| 申扎县| 威远县| 武定县| 高碑店市| 满洲里市| 镇巴县| 隆安县| 盘山县| 呼和浩特市| 巴彦淖尔市| 门源| 扎囊县| 宁阳县| 定陶县| 游戏| 浦县| 长海县| 双柏县| 铁岭市| 隆安县| 合水县| 磐石市| 郑州市| 靖西县| 平利县| 岫岩| 绥芬河市| 贞丰县| 临湘市| 三江| 任丘市| 武清区|