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

Creating interfaces, types aliases, and classes

In the Understanding basic types section, we introduced ourselves to objects, which are types that can have their own properties. Interfaces, type aliases, and classes are ways that we can define an object structure before we start using it.

Following here is the customer object we worked with, where we declared the customer variable with an initial object value:

const customer = {
name: "Lamps Ltd",
turnover: 2000134,
active: true
};
  1. Let's try to declare the customer variable and set its value on a subsequent line:
let customer: object;
customer = {
name: "Lamps Ltd",
turnover: 2000134,
active: true
};
  1. So far, so good. However, let's see what happens when we try to change the customers turnover value:
customer.turnover = 2000200;
  1. The lack of IntelliSense when we type turnover isn't what we are used to. When we've finished typing the line, we get a compiler error:

The TypeScript compiler doesn't know about the properties in the customer object and so thinks there's a problem.

So, we need another way of defining an object structure with the ability to set property values later in the program. That's where interfaces, type aliases, and classes come in; they let us define the structure of an object by letting us define our own types.

主站蜘蛛池模板: 红原县| 甘泉县| 金塔县| 湖南省| 商丘市| 石首市| 泰宁县| 富锦市| 鄂托克旗| 阿图什市| 腾冲县| 乐平市| 甘孜| 渝北区| 紫金县| 浦东新区| 霍城县| 京山县| 玉龙| 大厂| 浦江县| 克拉玛依市| 察哈| 江阴市| 邵东县| 沙田区| 广丰县| 五峰| 石楼县| 黎川县| 桃江县| 贵定县| 大埔区| 房产| 仁怀市| 峨眉山市| 崇文区| 鹤岗市| 新巴尔虎右旗| 四川省| 绥宁县|