Interface

描述:用来描述对象的形状,能够被继承

常用语法 ( Common Syntax )

1. 描述普通对象

interface JsonResponse {
version:number;
outOfStock?: boolean;
readonly body: string;
update: (retryTimes: number) => void;
update2(retryTimes: number):void
}
interface JsonResponse2 { [key: string]: number }

2. 描述函数

上个例子中,我们描述的是一个对象中拥有的一些属性。Interface 也可以直接来描述一个函数。

因为在 JS 中,一切皆是对象,函数在 JS 中也是对象,可以拥有属性,并且可以被调用。

interface JsonResponse {
(): string;
toFn: string
}
const fn: JsonResponse = () => {
return 'str'
}
fn.toFn = 'content'

3. 描述构造函数

还未搞懂...,有看到此随笔的朋友,推荐下相关链接,感谢‍

4. interface 的扩展

// 接口扩展接口
interface X { x: number }
interface Point extends X { y: number }
// Point {x:numer;y:number} // 也可扩展类型别名,同时可扩展多个
type Y = { y: number }
interface Point2 extends X, Y { z: number }
// Point2 {x:numer;y:number;z:number}
接口重名也会扩展,后续属性声明必须属于同一类型,否则报错!
interface Legged {
numberOfLegs: number;
}
interface Legged {
numberOfLegs: 123;
} // 报错,numberOfLegs 必须都为 number // ======= 在namespace 中也是如此
namespace Animals {
export interface Legged {
numberOfLegs: number;
}
}
namespace Animals {
export interface Legged {
numberOfHands: number;
}
}
// 合并为
namespace Animals {
export interface Legged {
numberOfLegs: number;
numberOfHands: number;
}
}

5. 附加注释,鼠标移入时编辑器会有附加注释

interface JsonResponse {
version: number
}
interface JsonResponse {
/** In bytes */
payloadSize: number
}

泛型( Generics )

interface APICall<R> {
data: R
}
// 使用
interface JsonResponse { content: string };
const api: APICall<JsonResponse> = { data: { content: 'xxx' } }
api.data.content

泛型约束

// 意味着要有 status 属性的类型才能使用
interface APICall<R extends { status: number }> {
data: R
}
// 使用
interface JsonResponse { content: string, status: number };
const api: APICall<JsonResponse> = { data: { content: 'xxx', status: 200 } }
api.data.status

重载( Overloads )

同样未搞懂...

Get & Set

可以描述对象的自定义 get 与 set 方法

interface Ruler {
get size(): number | string
set size(value: number | string)
} const ruler:Ruler = {
size: 123
}
ruler.size = 456
ruler.size = '456'
ruler.size // 类型为 string
// Error
// ruler.size = false

例子中 size 属性只能赋值 number 和 string 类型,由于第二次赋值为字符串,所以TypeScript自动推断返回值为 string,并不是 number | string

一致性类 ( Class conformance )

可通过 implements 关键字来确保类的一致性

interface Syncable { sync(): void }
class Account implements Syncable {
sync() { }
}
// 必须实现 sync 方法

感谢观看,欢迎互相讨论与指导,以下是参考资料链接

https://www.typescriptlang.org/static/TypeScript%20Interfaces-34f1ad12132fb463bd1dfe5b85c5b2e6.png

TypeScript 之 Interface的更多相关文章

  1. TypeScript之interface初探

    TypeScript的核心原则之一是对值所具有的结构进行类型检查,在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约. function printLabel(la ...

  2. 【区分】Typescript 中 interface 和 type

    在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...

  3. typescript 接口 interface

    代码: // 接口:行为的抽象 // 一.对class类的约束 // 接口定义 // 打印机 interface Iprinter { Printing(msg:string):string; } i ...

  4. Typescript的interface、class和abstract class

    interface,class,和abstract class这3个概念,既有联系,又有区别,本文尝试着结合官方文档来阐述这三者之间的关系. 1. Declaration Merging Declar ...

  5. typescript接口---interface

    假如我现在需要批量生产一批对象,这些对象有相同的属性,并且对应属性值的数据类型一致.该怎么去做? 在ts中,因为要检验数据类型,所以必须对每个变量进行规范,自然也提供了一种批量规范的功能.这个功能就是 ...

  6. Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)

    概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...

  7. [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types

    TypeScript has 'interface' and 'type', so when to use which? interface hasName { firstName: string; ...

  8. React + TypeScript 默认 Props 的处理

    React 中的默认 Props 通过组件的 defaultProps 属性可为其 Props 指定默认值. 以下示例来自 React 官方文档 - Default Prop Values: clas ...

  9. chrome插件: yapi 接口TypeScript代码生成器

    前言 2020-09-12 天气晴,蓝天白云,微风,甚好. 前端Jser一枚,在公司的电脑前,浏览器打开着yapi的接口文档,那密密麻麻的接口数据,要一个一个的去敲打成为TypeScript的inte ...

  10. TypeScript 面试题汇总(2020 版)

    TypeScript 面试题汇总(2020 版) TypeScript 3.9 https://www.typescriptlang.org/zh/ TypeScript 4.0 RC https:/ ...

随机推荐

  1. Kubernetes 安全

    RBAC 权限控制 对资源对象的操作都是通过 APIServer 进行的,那么集群是怎样知道我们的请求就是合法的请求呢?这个就需要了解 Kubernetes 中另外一个非常重要的知识点了:RBAC(基 ...

  2. Elasticsearch:Elasticsearch SQL介绍及实例(二)

    转载自:https://blog.csdn.net/UbuntuTouch/article/details/105699014

  3. C++ 自学笔记 访问限制 Setting limits

    Setting limits 让客户不能改,让设计者可以改 C++: 任何人访问 成员函数访问(同一个类的不同实例化对象可以相互访问私有成员变量) 类自己或子类访问 friend: 朋友就可以授权访问 ...

  4. 我的 Kafka 旅程 - 性能调优

    Producer 于 config/producer.properties 配置文件中的项 # 序列化数据压缩方式 [none/gzip/snappy/lz4/zstd] compression.ty ...

  5. C#-3 深入理解类

    一 类的概述(类是逻辑相关的数据和函数的封装,通常代表真实世界中或概念上的事物) 类是一种能存储数据并执行代码的数据结构,包含数据成员和函数成员. 数据成员存储类或类的实例相关的数据: 函数成员执行代 ...

  6. 洛谷P4513 小白逛公园 (线段树)

    这道题看起来像是线段树和最大子段和的结合,但这里求最大子段和不用dp,充分利用线段树递归的优势来处理.个人理解:线段树相当于把求整个区间的最大子段和的问题不断划分为很多个小问题,容易解决小问题,然后递 ...

  7. C#实现登录某web进而获取其token数据

    实习在学C#,记录一下学习过程! 首先是需求描述(基于C#的.net core MVC实现): User: Resource Owner Agent:Brower auth.brightspace.c ...

  8. springboot中使用mybatisplus自带插件实现分页

    springboot中使用mybatisplus自带插件实现分页 1.导入mybatisplus分页依赖 <dependency> <groupId>com.baomidou& ...

  9. 11.MongoDB系列之连接副本集

    1. Python连接副本集 from pymongo import MongoClient from bson.codec_options import CodecOptions from retr ...

  10. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...