一开始以为,需要使用 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. 理解OIDC协议和认证机制

    当互联网应用越来越多,每个应用程序都实现了自己的身份存储.认证和授权,用户需要在应用上反复的注册与登录,体验糟糕,用户身份信息无法在多个应用间共享与同步.当使用企业应用时,企业提供了一系列应用,尽管是 ...

  2. C语言break,return

    C语言break,continue,return的相似与区别 相同点: 都改变了程序的执行流程 区别是:break    用于循环和switch分支,跳出它所在分支或循环体到它所在的模块的      ...

  3. 前端工作面试HTML相关问题

    前端工作面试HTML相关问题 Q: doctype(文档类型)的作用是什么? A: 在HTML中 doctype 有两个主要目的. 对文档进行有效性验证: 它告诉用户代理和校验器这个文档是按照什么DT ...

  4. uni-app中实现图片左滑的效果

    template: 1 <view class="my-reg"> 2 <view class="my-regs"> 3 <ima ...

  5. Java类型转换详解

    Java类型转换详解 最近有同学问:自动类型转换老是记不住,到底是大转小,还是小转大 其实这个不用死记硬背,很好理解,我们拿 int 和 short 来举例: int 是 4 字节,也就是 32 bi ...

  6. Spring-Bean的依赖注入分析-01

    ###我们先通过一个例子弄明白为什么要使用依赖注入### 1.创建业务层UserService接口及UserServiceImpl实现类(接口代码省略) public class UserServic ...

  7. 深入剖析 RocketMQ 源码 - 负载均衡机制

    RocketMQ作为一款流行的消息中间件在各大互联网应用广泛,本文主要分析RocketMq在消息生产和消费过程中的负载均衡机制,并创新提出消费端负载均衡策略的改写以实现固定IP消费的可能.

  8. AgileConfig 1.6.0 发布 - 支持服务注册与发现

    大家好,好久没有输出博文了,一是因为比较忙,另外一个原因是最近主要的精力是在给 AgileConfig 添加一个新的功能:服务注册与发现. 先说说为什么会添加这个功能.我自己的项目是用 Consul ...

  9. java基础知识-序列化/反序列化-gson基础知识

    以下内容来之官网翻译,地址 1.Gson依赖 1.1.Gradle/Android dependencies { implementation 'com.google.code.gson:gson:2 ...

  10. STL空间配置器源码分析(四)bitmap_allocator

    一.摘要 bitmap_allocator是STL空间分配器的其中一种,它采用内存池策略,最多存储64条空闲链表(freelist,实际是一块空间连续的内存区,后面也称为超级块),每条空闲链表存储的内 ...