Hands-On Full Stack Web Development with Angular 6 and Laravel 5
上QQ阅读APP看书,第一时间看更新

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.