一开始以为,需要使用 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. Linux 中进程有哪几种状态?在 ps 显示出来的信息中, 分别用什么符号表示的?

    1.不可中断状态:进程处于睡眠状态,但是此刻进程是不可中断的.不可中断, 指进程不响应异步信号. 第 441 页 共 485 页2.暂停状态/跟踪状态:向进程发送一个 SIGSTOP 信号,它就会因响 ...

  2. 《自动控制原理》个人笔记(来自ppt课件)

    控制的含义 控制(CONTROL)----某个主体使某个客体按照一定的目的动作.主体–人:人工控制: 机器:自动控制客体–指一件物体,一套装置,一个物化过程,一个特定系统. 人工控制与自动控制 人在控 ...

  3. ZEGO音视频服务的高可用架构设计与运营

    前言: ZEGO 即构科技作为一家实时音视频的提供商,系统稳定性直接影响用户的主观体验,如何保障服务高可用且用户体验最优是行业面临的挑战,本文结合实际业务场景进行思考,介绍 ZEGO 即构在高可用架构 ...

  4. H5: 表单验证失败的提示语

    前言     前端的童鞋在写页面时, 都不可避免的总会踩到表单验证这个坑. 这时候, 我们就要跪了, 因为要写一堆js来检查. 但是自从H5出现后, 很多常见的表达验证, 它都已经帮我们实现了, 让我 ...

  5. 夏日葵电商:连锁零售店小程序o2o系统解决方案

    公众平台"附近小程序"功能上线后,一个主体账号可以同时绑定N+个门店,这对连锁零售店铺来说是重磅福利呀,无论你是通过搜索还是线下扫码进入小程序,线上与线下都完全贯通了,线上多种入口 ...

  6. C2678 二进制“<”: 没有找到接受“const ***”类型的左操作数的运算符解决办法

    正确代码如下:#include<iostream> #include<string> #include<map> using namespace std; /*仿函 ...

  7. 小程序申请测试appid

    话不多说,直接上图:  1. 登录微信官方文档: https://developers.weixin.qq.com/miniprogram/dev/devtools/sandbox.html  2. ...

  8. vue 多级组件数据传递

    A包含B组件,B包含C组件   那么A 传递到C 组件可以通过 在B组件中绑定 $attrs 具体代码可以参见github: https://github.com/qiaoqiao10001/vueA ...

  9. 【C++】二叉树的遍历(前中后)- 迭代法

    力扣题目:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 今天自己琢磨了很久如何不用递归将二叉树的遍历写出来,于是乎写出 ...

  10. Azure DevOps (九) 通过流水线推送镜像到Registry

    上一篇文章我们研究了如何通过流水线编译出一个docker的镜像,本篇我们来研究一下,如何把编译好的镜像推送到镜像仓库去. 平时如果我们是单机部署,我们的docker本身就装在部署的机器上,我们在本机直 ...