被迫开始学习Typescript —— interface
一开始以为,需要使用 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的更多相关文章
- 被迫开始学习Typescript —— vue3的 props 与 interface
vue3 的 props Vue3 的 props ,分为 composition API 的方式以及 option API 的方式,可以实现运行时判断类型,验证属性值是否符合要求,以及提供默认值等功 ...
- 被迫开始学习Typescript —— class
TS 的 class 看起来和 ES6 的 Class 有点像,基本上差别不大,除了 可以继承(实现)接口.私有成员.只读等之外. 参考:https://typescript.bootcss.com/ ...
- Avalon总线学习 ---Avalon Interface Specifications
Avalon总线学习 ---Avalon Interface Specifications 1.Avalon Interfaces in a System and Nios II Processor ...
- 在WisOne平台上学习TypeScript
TypeScript是微软公司推出的开源的类型化脚本语言,目的是用于为弱类型的javaScript提供强类型的识别和感知功能,同时它提供了类.接口.继承等相关在javaScript中不容易实现的功能, ...
- 学习TypeScript,笔记一:TypeScript的简介与数据类型
该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27 16:50:03 一.什么是TypeScript? TypeScript是javasc ...
- 学习typescript(二)
学习typescript(二) ts 与 js 交互 ts 调用 js module使用 分为两种情况: ts 调用自己写的 js ts 调用别人写的 js 也就通过 npm 安装的 第一种情况处理如 ...
- TypeScript Interface vs Types All In One
TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...
- TypeScript Interface(接口)
类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...
- 【One by one系列】一步步学习TypeScript
TypeScript Quick Start 1.TypeScript是什么? TypeScript是ES6的超集. TS>ES7>ES6>ES5 Vue3.0已经宣布要支持ts,至 ...
随机推荐
- 学习k8s(四)
1.K8S核心组件 1.Master节点: etcd: 分布式键值对数据库,保存集群状态 api-server: 接受并响应用户的请求 controller: 控制器管理,控制容器的副本数,故障检测 ...
- 创建axios拦截器
上一篇说axios并发的时候有提到 axios的请求统一管理是为了创建拦截器 具体说一下拦截器的创建 import Vue from 'vue'; import axios from 'axios'; ...
- h5 在全屏iphonex中的适配
iphonex 已经上线有一段时间了,作为业界刘海屏幕第一款机型,导致全屏不能正常的全屏显示了,,所以需要对iphonx 适配,下面就详细说说如何适配 先看一张适配前后的图: iphonex 提供的 ...
- python-使用函数求特殊a串数列和
给定两个均不超过9的正整数a和n,要求编写函数fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和 函数接口定义: 1 fn(a,n) 2 其中 a 和 n 都是用 ...
- java中抽象类和抽象方法到底有什么用呢?
抽象类和抽象方法有什么用呢?马克-to-win:当初sun公司为什么要设计抽象类和抽象方法呢?当你在做车的系统设计时,当你设计车这个通用类时,假如你确认别人实例化车这个通用类没有意义时(不知道是bik ...
- mysql基本操作2
##DDL控制表结构,不支持事务##DML控制表数据,支持事务 DQL专门做查询 ##TCL 管理事务##DCL 管理数据库权限 ##ORDER BY 子句-根据指定列对结果集 ...
- Exchange日志清理
1.清理日志--完整备份 Exchange Server 2013被部署在Windows Server 2012 及以上版本的操作系统中,使用操作系统内的"Windows Server Ba ...
- mongodb安装错误以及原理
安装mongodb,默认是安装到"C:\Program Files\MongoDB\"这里的,我在注册表里没有找到mongodb的信息,所以猜测它只是将其解压到那个位置而已,它只是 ...
- Java安全之Commons Collections6分析
Java安全之Commons Collections6分析 0x00 前言 其实在分析的几条链中都大致相同,都是基于前面一些链的变形,在本文的CC6链中,就和前面的有点小小的区别.在CC6链中也和CC ...
- 机器学习系列:LightGBM 可视化调参
大家好,在100天搞定机器学习|Day63 彻底掌握 LightGBM一文中,我介绍了LightGBM 的模型原理和一个极简实例.最近我发现Huggingface与Streamlit好像更配,所以就开 ...