简介
image对象和数据结构
使用getters
和setters
TypeScript 支持 getter/setter 语法。使用 getter 和 setter 从对象中访问数据比简单地在对象上查找属性要好。原因如下:
- 当需要在获取对象属性之前做一些事情时,不必在代码中查找并修改每个访问器。
- 执行
set
时添加验证更简单。 - 封装内部表示。
- 更容易添加日志和错误处理。
- 可以延迟加载对象的属性,比如从服务器获取它。
反例:
class BankAccount {
balance: number = 0;
// ...
}
const value = 100;
const account = new BankAccount();
if (value < 0) {
throw new Error('Cannot set negative balance.');
}
account.balance = value;
正例:
class BankAccount {
private accountBalance: number = 0;
get balance(): number {
return this.accountBalance;
}
set balance(value: number) {
if (value < 0) {
throw new Error('Cannot set negative balance.');
}
this.accountBalance = value;
}
// ...
}
const account = new BankAccount();
account.balance = 100;
让对象拥有 private/protected 成员
TypeScript 类成员支持 public
(默认)、protected
以及 private
的访问限制。
反例:
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
perimeter(){
return 2 * Math.PI * this.radius;
}
surface(){
return Math.PI * this.radius * this.radius;
}
}
正例:
class Circle {
constructor(private readonly radius: number) {
}
perimeter(){
return 2 * Math.PI * this.radius;
}
surface(){
return Math.PI * this.radius * this.radius;
}
}
不变性
TypeScript 类型系统允许将接口、类上的单个属性设置为只读,能以函数的方式运行。
反例:
interface Config {
host: string;
port: string;
db: string;
}
正例:
interface Config {
readonly host: string;
readonly port: string;
readonly db: string;
}
类型 vs 接口
当可能需要联合或交集时,请使用类型。如果需要扩展
或实现
,请使用接口。然而,没有严格的规则,只有适合的规则。
反例:
interface EmailConfig {
// ...
}
interface DbConfig {
// ...
}
interface Config {
// ...
}
//...
type Shape {
// ...
}
正例:
type EmailConfig {
// ...
}
type DbConfig {
// ...
}
type Config = EmailConfig | DbConfig;
// ...
interface Shape {
}
class Circle implements Shape {
// ...
}
class Square implements Shape {
// ...
}