一开始以为,需要使用 class 来定义呢,学习之后才发现,一般都是使用 interface 来定义的。

这个嘛,倒是挺适合 js 环境的。

参考:https://typescript.bootcss.com/interfaces.html

简单接口

我们先来定义一个简单的接口

interface Person {
name: string,
age: number
}

用接口定义一个对象

const jim: Person = {
name: 'jyk',
age: 18
}

这样编辑器就可以按照接口的定义来检查 jim 是否符合要求。

嵌套的情况

如果是多层的属性怎么办呢?我们可以一起定义,也可以分开定义。

  • 在一起定义(比较直观):
interface Person {
name: string,
age: number,
role: {
api: string,
moduleId: number
}
}
  • 分开定义(可以灵活组合):
interface Role {
api: string,
moduleId: number
} interface Person {
name: string,
age: number,
role: Role
}

数组和索引

如果有多个 role 呢,我们可以用数组的形式,也可以用索引的方式。

  • 数组的方式
interface Person {
name: string,
age: number,
roles: Array<Role>
}
  • 索引的方式
interface Person {
name: string,
age: number,
roles: {
[index: number | string ]: Role
}
}

可以有其他任何属性

js 可以很随意,没想到 ts 也可以比较随意。

interface SquareConfig {
color: string;
width: number;
[propName: string]: any;
}

除了指定的属性之外,还可以有其他任意属性。这个嘛。

函数类型

interface SearchFunc {
(source: string, subString: string): boolean;
}

定义参数和返回类型。

接口的合并

这个嘛,说起来挺奇怪的,虽然是一个我想要的的方式,但是发现真的有这种方式的时候,还是感觉挺诧异的。

interface StateOption {
isLocal?: boolean,
isLog?: boolean,
level?: number
} interface StateCreateOption {
state?: any,
getters?: any,
actions?: any
} const foo: StateCreateOption & StateOption = {
isLocal: true,
state: {},
}

可以把两个接口合并在一起,约束一个对象,要求有两个接口的属性。

贯彻了 js 世界里的 “组合>继承” 的特点。

接口继承接口

接口除了合并之外,还可以继承接口,支持多继承。

interface Customer extends Person, 其他接口 {
phone: string
}

类继承(实现)接口

ts 的 class 也可以继承(实现)接口,也支持多继承。

class Customer extends Person, 其他接口 {
phone: string
}

接口继承类

接口还可以继承类?我也没想到可以这样。

class Control {
private state: any;
} interface SelectableControl extends Control {
select(): void;
} class Button extends Control implements SelectableControl {
select() { }
} class TextBox extends Control { } // Error: Property 'state' is missing in type 'Image'.
class Image implements SelectableControl {
select() { }
} class Location { }

是不是挺绕的?好吧,我也没绕出来。

小结

  • 继承 class 使用 extends。
  • 继承 interface 使用 implements。
  • 既有约束,也有一定的灵活性。

被迫开始学习Typescript —— interface的更多相关文章

  1. 被迫开始学习Typescript —— vue3的 props 与 interface

    vue3 的 props Vue3 的 props ,分为 composition API 的方式以及 option API 的方式,可以实现运行时判断类型,验证属性值是否符合要求,以及提供默认值等功 ...

  2. 被迫开始学习Typescript —— class

    TS 的 class 看起来和 ES6 的 Class 有点像,基本上差别不大,除了 可以继承(实现)接口.私有成员.只读等之外. 参考:https://typescript.bootcss.com/ ...

  3. Avalon总线学习 ---Avalon Interface Specifications

    Avalon总线学习 ---Avalon Interface Specifications 1.Avalon Interfaces in a System and Nios II Processor ...

  4. 在WisOne平台上学习TypeScript

    TypeScript是微软公司推出的开源的类型化脚本语言,目的是用于为弱类型的javaScript提供强类型的识别和感知功能,同时它提供了类.接口.继承等相关在javaScript中不容易实现的功能, ...

  5. 学习TypeScript,笔记一:TypeScript的简介与数据类型

    该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27  16:50:03 一.什么是TypeScript? TypeScript是javasc ...

  6. 学习typescript(二)

    学习typescript(二) ts 与 js 交互 ts 调用 js module使用 分为两种情况: ts 调用自己写的 js ts 调用别人写的 js 也就通过 npm 安装的 第一种情况处理如 ...

  7. TypeScript Interface vs Types All In One

    TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...

  8. TypeScript Interface(接口)

    类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...

  9. 【One by one系列】一步步学习TypeScript

    TypeScript Quick Start 1.TypeScript是什么? TypeScript是ES6的超集. TS>ES7>ES6>ES5 Vue3.0已经宣布要支持ts,至 ...

随机推荐

  1. ArrayList、LinkedList、Vector、Array

    ArrayList 本质是一个数组. 优势:追加元素到数组末尾的时候速度快,同时检索元素的速度也快. 劣势:如果要插入一个元素到数组之间慢:如果要追加的元素数量多于数组的容量,则需要频繁扩容使用Arr ...

  2. 【Matlab】简单的滑模控制程序及Simulink仿真

    文章: [控制理论]滑模控制最强解析 滑模控制程序及Simulink仿真 这篇文章仿真和输出U的推到有些问题,博主根据此篇文章进行修改进行对sin(t)曲线的追踪(使用滑模控制) 使用滑模控制对sin ...

  3. Asp.Net Core之Identity应用(下篇)

    一.前言 在上篇中简单介绍了 Asp.Net Core 自带的 Identity,一个负责对用户的身份进行认证的框架,当我们按需选择这个框架作为管理和存储我们应用中的用户账号数据的时候,就会添加到自己 ...

  4. centos报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/

    检查是否可以上网. ping 114.114.114.114 如果不可以,调试通.通了之后下一步: 然后检查DNS设置是否正常. ping www.baidu.com 不正常的话,设置DNS,如下: ...

  5. Exchange日志清理

    1.清理日志--完整备份 Exchange Server 2013被部署在Windows Server 2012 及以上版本的操作系统中,使用操作系统内的"Windows Server Ba ...

  6. 微信小程序命名规则

    目录分析 src是主要的开发目录,各个文件实现功能如下所示: ├─.idea │ └─libraries ├─.temp ├─config └─src ├─assets │ └─images ├─co ...

  7. 《头号玩家》AI电影调研报告(三)

    [AR市场正在迅猛增长] 据<工业增强现实现状2017>报告中所述,AR不再只是值得期待的新兴技术.2018年,投资此类技术已成为很多组织机构的关键战略,尤其是对于涉及复杂的制造和运营流程 ...

  8. java——封装

    java--封装 java--封装1 封装的理解和好处2 封装的事项实现步骤3 将构造器和setXx结合4 this和super区分 1 封装的理解和好处 隐藏实现细节:[方法(连接数据库)<- ...

  9. Cocos Creator绕远做圆周运动,且变换运动物体的角度

    需求:绕远做圆周运动 并且精灵的角度要随着位置的改变而改变 网上有很多做圆周运动的代码,但是要不然就是角度不变 要不然就是cocos版本老旧 摘了一段3.x的圆周运动,自己加了角度变换 圆周运动,已知 ...

  10. 通过nfs将centos目录挂载到windows 系统的磁盘上

    环境:centos8,windows7 1.在centos上安装nfs服务 yum -y install nft-utils 2.启动nfs服务 systemctl start nfs-server ...