- Learn React with TypeScript 3
- Carl Rippon
- 216字
- 2021-06-10 19:16:33
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
};
- 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
};
- So far, so good. However, let's see what happens when we try to change the customers turnover value:
customer.turnover = 2000200;
- 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.
推薦閱讀
- Python概率統(tǒng)計(jì)
- Getting started with Google Guava
- Beginning C++ Game Programming
- JavaScript 網(wǎng)頁編程從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- JavaScript前端開發(fā)與實(shí)例教程(微課視頻版)
- Java程序設(shè)計(jì)與實(shí)踐教程(第2版)
- C語言程序設(shè)計(jì)
- Mastering Akka
- 移動(dòng)增值應(yīng)用開發(fā)技術(shù)導(dǎo)論
- C陷阱與缺陷
- IoT Projects with Bluetooth Low Energy
- Go語言入門經(jīng)典
- Android編程權(quán)威指南(第4版)
- 高性能PHP 7
- PHP Microservices