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

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.

主站蜘蛛池模板: 连山| 德惠市| 大余县| 九江县| 彰化县| 徐州市| 福贡县| 洱源县| 梅河口市| 新绛县| 柘城县| 紫阳县| 德惠市| 漳浦县| 红安县| 清镇市| 深水埗区| 南靖县| 颍上县| 石嘴山市| 吴忠市| 亳州市| 普陀区| 扶余县| 宜阳县| 枝江市| 永兴县| 安吉县| 四平市| 舒兰市| 思南县| 安仁县| 青州市| 临城县| 广南县| 措勤县| 西充县| 宁阳县| 凤阳县| 革吉县| 阜城县|