惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
V
V2EX
Jina AI
Jina AI
S
Schneier on Security
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
美团技术团队
小众软件
小众软件
L
LangChain Blog
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
T
Threatpost
T
Tor Project blog
K
Kaspersky official blog
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
H
Heimdal Security Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
N
News | PayPal Newsroom
I
Intezer
博客园 - 聂微东
U
Unit 42
Cisco Talos Blog
Cisco Talos Blog
量子位
T
The Exploit Database - CXSecurity.com
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
Webroot Blog
Webroot Blog
I
InfoQ
The Cloudflare Blog
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
Hacker News: Ask HN
Hacker News: Ask HN
IT之家
IT之家
Google DeepMind News
Google DeepMind News
C
Cisco Blogs

博客园 - 金色海洋(jyk)

【vue3】详解单向数据流,大家千万不用为了某某而某某了。 Vue3.3 的新功能的体验(下):泛型组件(Generic Component) 与 defineSlots Vue3.3 的新功能的一些体验 Vue3实现组件级基类的几种方法 【摸鱼神器】UI库秒变低代码工具——表单篇(二)子控件 【摸鱼神器】UI库秒变低代码工具——表单篇(一)设计 用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。 【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具 【摸鱼神器】UI库秒变LowCode工具——列表篇(一)设计与实现 【摸鱼神器】一次搞定 vue3的 路由 + 菜单 + tabs 被迫开始学习Typescript —— vue3的 props 与 interface 被迫开始学习Typescript —— class 结合 Vuex 和 Pinia 做一个适合自己的状态管理 nf-state 一篇文章说清 webpack、vite、vue-cli、create-vue 的区别 从 jQuery 到 Vue3 的快捷通道 简单了解一下pinia的结构 nf-Press —— 在线文档也可以加载组件和编写代码 vite2 打包的时候vendor-xxx.js文件过大的解决方法 基于 vite2 + Vue3 写一个在线帮助文档工具
被迫开始学习Typescript —— interface
金色海洋(jyk) · 2022-05-14 · via 博客园 - 金色海洋(jyk)

2022-05-14 10:11  金色海洋(jyk)  阅读(571)  评论()    收藏  举报

一开始以为,需要使用 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。
  • 既有约束,也有一定的灵活性。