TS入门篇 | 详解 TypeScript 类类型(上)

简介: 传统的面向对象语言都是基于类的,而JavaScript是基于原型的。在ES6中拥有了class关键字,虽然它的本质依旧是构造函数,但是能够让开发者更舒服的使用class了。 TypeScript 作为 JavaScript 的超集,自然也是支持 class 全部特性的,并且还可以对类的属性、方法等进行静态类型检测。下面就来看看 TypeScript 中的类类型。

一、类的概念


1. 类的使用


在开发过程中,任何实体都可以被抽象为一个使用类表达的类似对象的数据结构,这个数据结构既包含属性,又包含方法。在TypeScript 中可以这样来抽象一个坐标点类:

class Point {
  x: number;
  y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
  getPosition() {
    return `(${this.x}, ${this.y})`;
  }
}
const point = new Point(1, 2);
point.getPosition()   // (1, 2)
复制代码


这里定义了一个 Point 坐标点类,它拥有两个number类型的属性 x 和 y,一个构造器函数和一个getPosition方法。后面通过new实例化了一个Point,并将实例赋值给变量point,最后通过实例调用了类中定义的 getPosition 方法。

在ES6之前,需要使用函数+原型链的形式进行模拟定义类:

function Point(x, y) {
  this.x = x;
  this.y = y;
}
Point.prototype.getPosition = function() {
  return `(${this.x}, ${this.y})`;
}
const point = new Point(1, 2);
point.getPosition()   // (1, 2)
复制代码


开始定义了 Point 类的构造函数,并在构造函数内部定义了 x 和 y 属性,后面通过 Point 的原型链添加了 getPosition 方法。这样也模拟实现了 class 的功能,但是看起来麻烦很多,并且缺少静态类型检测。因此,类是 TypeScript 编程中十分有用且不得不掌握的工具。


2. 类的继承


下面来看一下作为面向对象的三大也行之一的继承,在 TypeScript 中,可以使用 extends 关键字来定义类继承的抽象模式:

class A {
  name: string;
  age: number;
  constructor(name: string, age: number) {
      this.name = name;
      this.age = age;
  }
  getName() {
      return this.name;
  }
}
class B extends A {
  job: string;
  constructor(name: string, age: number) {
      super(name, age);
      this.job = "IT";
  }
  getJob() {
      return this.job;
  }
  getNameAndJob() {
      return super.getName() + this.job;
  }
}
var b = new B("Tom", 20);
console.log(b.name);
console.log(b.age);
console.log(b.getName());
console.log(b.getJob());
console.log(b.getNameAndJob());
//输出:Tom,20,Tom,IT,TomIT
复制代码


如上,B继承A,那A被称为父类(超类),B被称为子类(派生类)。这就是类最基本的继承用法,B就是一个派生类,它派生自A类,此时B的实例继承了基类A的属性和方法。因此,实例 b 支持 name、age、getName 等属性和方法。


需要注意,派生类如果包含一个构造函数constructor,则必须在构造函数中调用 super() 方法,这是 TypeScript 强制执行的一条重要规则。否则就会报错:Constructors for derived classes must contain a 'super' call.


那这个 super() 有作用呢?其实这里的 super 函数会调用基类的构造函数,当我们把鼠标放在super方法上面时,可以看到一个提示,它的类型是基类 A 的构造函数:constructorA(name: string,age: number): A。并且指明了需要传递两个参数,不然TypeScript就会报错。


二、类的修饰符


1. 访问修饰符


在 ES6 标准类的定义中,默认情况下,定义在实例的属性和方法会在创建实例后添加到实例上;而如果是定义在类里没有定义在 this 上的方法,实例可以继承这个方法;而如果使用 static 修饰符定义的属性和方法,是静态属性和静态方法,实例是没法访问和继承到的。


传统面向对象语言通常都有访问修饰符,可以通过修饰符来控制可访问性。TypeScript 中有三类访问修饰符:

  • public:修饰的是在任何地方可见、公有的属性或方法;
  • private:修饰的是仅在同一类中可见、私有的属性或方法;
  • protected:修饰的是仅在类自身及子类中可见、受保护的属性或方法。


(1)public

public表示公共的,用来指定在创建实例后可以通过实例访问的,也就是类定义的外部可以访问的属性和方法。默认是 public,但是 TSLint 可能会要求必须用限定符来表明这个属性或方法是什么类型的:


class Point {
  public x: number;
  public y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
  public getPosition() {
    return `(${this.x}, ${this.y})`;
  }
}
const point = new Point(1, 2)
console.log(point.x)   // 1
console.log(point.y)   // 2
console.log(point.getPosition())  // (1, 2)
复制代码


(2)private

private修饰符表示私有的,它修饰的属性在类的定义外面是没法访问的:


class Parent {
  private age: number;
  constructor(age: number) {
    this.age = age;
  }
}
const p = new Parent(18);
console.log(p);  // { age: 18 }
console.log(p.age); // error Property 'age' is private and only accessible within class 'Parent'.
console.log(Parent.age); // error Property 'age' does not exist on type 'typeof Parent'.
class Child extends Parent {
  constructor(age: number) {
    super(age);
    console.log(super.age); // Only public and protected methods of the base class are accessible via the 'super' keyword.
  }
}
复制代码


这里 age 属性使用 private 修饰符修饰,说明它是私有属性,打印创建的实例对象 p,发现它是有属性 age 的,但是当试图访问 p 的 age 属性时,编译器会报错,私有属性只能在类 Parent 中访问。


对于 super.age 的报错,在不同类型的方法里 super 作为对象代表着不同的含义,这里在 constructor 中访问 super,这的 super 相当于父类本身,使用 private 修饰的属性,在子类中是无法访问的。


(3) protected

protected是受保护修饰符,和private有些相似,但有一点不同,protected修饰的成员在继承该类的子类中可以访问。把上面那个例子父类 Parent 的 age 属性的修饰符 private 替换为


class Parent {
  protected age: number;
  constructor(age: number) {
    this.age = age;
  }
  protected getAge() {
    return this.age;
  }
}
const p = new Parent(18);
console.log(p.age); // error Property 'age' is protected and only accessible within class 'Parent' and its subclasses.
console.log(Parent.age); // error Property 'age' does not exist on type 'typeof Parent'.
class Child extends Parent {
  constructor(age: number) {
    super(age);
    console.log(super.age); // error Only public and protected methods of the base class are accessible via the 'super' keyword.
    console.log(super.getAge());
  }
}
new Child(18)
复制代码


protected还能用来修饰 constructor 构造函数,加了protected修饰符之后,这个类就不能再用来创建实例,只能被子类继承,ES6 的类需要用new.target来自行判断,而 TS 则只需用 protected 修饰符即可:


class Parent {
  protected constructor() {
    //
  }
}
const p = new Parent(); // error Constructor of class 'Parent' is protected and only accessible within the class declaration.
class Child extends Parent {
  constructor() {
    super();
  }
}
const c = new Child();
复制代码


相关文章
|
30天前
|
JavaScript 前端开发
【TypeScript入门】TypeScript入门篇——数据类型
我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’的区别的,因此,在每个编程语言里都会有一个叫数据类型的东东,其实就是对常用的各种数据类型进行了明确的划分,你想让计算机进行数值运算,你就传数字给它,你想让他处理文字,就传字符串类型给他。
17 3
|
2月前
|
JavaScript 前端开发 安全
Apollo与TypeScript:强大类型检查在前端开发中的应用
Apollo与TypeScript:强大类型检查在前端开发中的应用
|
3月前
|
JavaScript 前端开发 安全
ES6的类 vs TypeScript的类:解密两种语言中的面向对象之争
ES6的类 vs TypeScript的类:解密两种语言中的面向对象之争
|
3天前
|
JavaScript 前端开发 编译器
TypeScript中的高级类型:联合类型、交叉类型与条件类型深入解析
【4月更文挑战第23天】探索TypeScript的高级类型。这些特性增强类型系统的灵活性,提升代码质量和维护性。
|
1月前
|
JavaScript 安全
TypeScript 中的高级类型转换技术:映射类型、条件类型和类型推断
TypeScript 中的高级类型转换技术:映射类型、条件类型和类型推断
|
1月前
react+typescript给state和props定义指定类型
react+typescript给state和props定义指定类型
16 1
|
2月前
|
JavaScript 前端开发 开发者
JavaScript(JS)和TypeScript(TS)的区别
JavaScript(JS)和TypeScript(TS)的区别
29 0
|
2月前
|
JavaScript
【HarmonyOS 4.0 应用开发实战】TypeScript入门之声明、数据类型、函数、类的详讲
【HarmonyOS 4.0 应用开发实战】TypeScript入门之声明、数据类型、函数、类的详讲
44 0
|
2月前
|
JavaScript 安全 索引
TypeScript泛型和类型体操
泛型和类型体操(Type Gymnastics)是 TypeScript 中高级类型系统的重要组成部分。它们提供了强大的工具和技巧,用于处理复杂的类型操作和转换。
|
2月前
|
JavaScript 安全 容器
Vue3 + setup + TypeScript: 构建现代、类型安全的Vue应用的关键技巧总结
当使用 setup 的时候,组件直接引入就可以了,不需要再自己手动注册