TypeScript 学习笔记(五):interface接口

前言

在上一篇文章中我们学习了TS中的数组和元组,本篇文章我们主要了解TS中的interface接口。

interface接口介绍

interface 是一种类型约定,中文译为"接口"。使用了这个interface的对象,就拥有了指定的类型结构。

TS 复制代码
interface Person {
  firstName: string;
  lastName: string;
}

上面示例中,定义了一个接口Person,它指定一个对象模板,拥有属性firstNamelastName任何实现这个接口的对象,都必须部署这两个属性。 如以下实例,变量p的类型就是接口Person,所以必须符合Person指定的结构,否则就会报错。

TS 复制代码
const p:Person = {
  firstName: 'Li',
  lastName: 'Xu',
};

interface 的使用

interface接口中,一般有着以下几类成员。

  • 对象属性
  • 对象的属性索引
  • 对象方法
  • 函数

对象属性

示例:

TS 复制代码
interface Point {
  x: number;
  y: number;
}

如果属性是可选的,就在属性名后面加一个问号。

TS 复制代码
interface Foo {
  x?: string;
}

如果属性是只读的,需要加上readonly修饰符。

TS 复制代码
interface A {
  readonly a: string;
}

对象的属性索引

TS 复制代码
interface A {
  [Key: string]: number;
}

上面示例中,[Key: string]就是属性的字符串索引,表示属性名只要是字符串,都符合类型要求。属性索引共有stringnumbersymbol三种类型。

对象方法

写法如下:

TS 复制代码
// 写法一
interface A {
  f(x: boolean): string;
}

// 写法二
interface B {
  f: (x: boolean) => string;
}

// 写法三
interface C {
  f: { (x: boolean): string };
}

这三种写法都定义了一个接受布尔类型参数并返回字符串类型的函数。

函数

interface也可以用来声明独立的函数。

TS 复制代码
interface Add {
  (x:number, y:number): number;
}

const myAdd:Add = (x,y) => x + y;

interface的继承

interface可以继承其他类型。

interface继承interface

interface可以使用extends关键字,继承其他interface

TS 复制代码
interface Shape {
  name: string;
}

interface Circle extends Shape {
  radius: number;
}

extends关键字会从继承的接口里面拷贝属性类型,这样就不必书写重复的属性。

而且interface还允许多重继承,如以下例子。

TS 复制代码
interface Style {
  color: string;
}

interface Shape {
  name: string;
}

interface Circle extends Style, Shape {
  radius: number;
}

如果子接口与父接口存在同名属性,那么子接口的属性会覆盖父接口的属性。但是子接口与父接口的同名属性必须是类型兼容的,不能有冲突。

TS 复制代码
interface Foo {
  id: string;
}

interface Bar extends Foo {
  id: number; // 报错
}

interface继承type

interface可以继承type命令定义的对象类型。如果type命令定义的类型不是对象,interface 就无法继承。

TS 复制代码
type Country = {
  name: string;
  capital: string;
}

interface CountryWithPop extends Country {
  population: number;
}

interface继承class

TS 复制代码
class A {
  x:string = '';

  y():boolean {
    return true;
  }
}

interface B extends A {
  z: number
}

interfacetype的区别

interface命令与type命令作用类似,都可以表示对象类型。

很多对象类型既可以用 interface 表示,也可以用 type 表示。而且,两者往往可以换用,几乎所有的 interface 命令都可以改写为 type 命令。

相同点

都能为对象类型起名。

TS 复制代码
type Country = {
  name: string;
  capital: string;
}

interface Country {
  name: string;
  capital: string;
}

不同点

  1. type能够表示非对象类型,而interface只能表示对象类型。
  2. interface可以继承其他类型,type不支持继承。
  3. 同名interface会自动合并,同名type则会报错。也就是说,TypeScript 不允许使用type多次定义同一个类型。
  4. interface不能包含属性映射,type可以。
  5. this关键字只能用于interface
  6. type 可以扩展原始数据类型,interface 不行。
相关推荐
ssshooter38 分钟前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
兆子龙1 小时前
【React】19 深度解析:掌握新一代 React 特性
前端·架构
Moment1 小时前
MinIO已死,MinIO万岁
前端·后端·github
无双_Joney1 小时前
心路散文 - 转职遇到AI浪潮,AIGC时刻人的价值是什么?
前端·后端·架构
有意义2 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
小怪点点2 小时前
vue3使用
前端·vue.js
Bigger2 小时前
CSS 这些年都经历了什么?一次看懂 CSS 的演化史
前端·css·前端工程化
DevUI团队2 小时前
🚀 【Angular】MateChat V20.2.2版本发布,新增8+组件,欢迎体验~
前端·javascript·人工智能
嚴寒2 小时前
前端配环境配到崩溃?这个一键脚手架让我少掉了一把头发
前端·react.js·架构
DevUI团队3 小时前
🚀 MateChat V1.11.0 震撼发布!新增工具按钮栏组件及体验问题修复,欢迎体验~
前端·javascript·人工智能