TypeScript之interface初探
TypeScript的核心原则之一是对值所具有的结构进行类型检查,在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。
function printLabel(labelObj: {label: String}){
console.log(labelObj.label);
}
let myObj = {name: 'Hi', label: "See you agin"};
printLabel(myObj);
用Interface重写以上例子
interface labelObjType {
label: String;
}
function printLabel2(labelObj: {label: String}){
console.log(labelObj.label);
}
printLabel2(myObj);
可选属性
function createSquare(Config: SquareConfig): {color: String; area: number}{
let newSquare = {color: 'white', area: 100};
if(Config.color){
newSquare.color = Config.color;
}
if(Config.width){
newSquare.area = Config.width*Config.width;
}
return newSquare;
}
let Square = createSquare({color: 'red', width: 20});
console.log(Square);
只读属性
TypeScript具有ReadonlyArray类型,它与Array相似,只是把所有可变方法去掉了,因此可以确保数组创建后再也不能被修改:
interface Point {
readonly x: number;
readonly y: String;
}
let obj: Point = {x: 10, y: 'Jomsou'};
//obj.x = 52;//error
let a: number[] = [1, 2, 54, 65, 41];
let b: ReadonlyArray<number> = a;
a[0] = 24;
//b[0] = 25;//error
//b.length = 10;//error
//b.push(10);//error
//a = b;//error
a = b as number[];//上面代码的最后一行,可以看到就算把整个ReadonlyArray赋值到一个普通数组也是不可以的。 但是你可以用类型断言重写:
绕过检查方法
let mySquare = createSquare({ colour: "red", width: 100 });
- 用类型断言重写:
let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig);
- 添加一个字符串索引签名
前提是你能够确定这个对象可能具有某些做为特殊用途使用的额外属性。 如果 SquareConfig带有上面定义的类型的color和width属性,并且还会带有任意数量的其它属性,那么我们可以这样定义它:
[propName: string]: any;
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}
- 它就是将这个对象赋值给一个另一个变量: 因为 squareOptions不会经过额外属性检查,所以编译器不会报错。
let squareOptions = { colour: "red", width: 100 };
let mySquare = createSquare(squareOptions);
函数类型
接口能够描述JavaScript中对象拥有的各种各样的外形。 除了描述带有属性的普通对象外,接口也可以描述函数类型。
为了使用接口表示函数类型,我们需要给接口定义一个调用签名。 它就像是一个只有参数列表和返回值类型的函数定义。参数列表里的每个参数都需要名字和类型。
interface SearchFunc {
(source: string, subString: string): boolean;
}
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
let result = source.search(subString);
return result > -1;
}
TypeScript之interface初探的更多相关文章
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- typescript 接口 interface
代码: // 接口:行为的抽象 // 一.对class类的约束 // 接口定义 // 打印机 interface Iprinter { Printing(msg:string):string; } i ...
- Typescript的interface、class和abstract class
interface,class,和abstract class这3个概念,既有联系,又有区别,本文尝试着结合官方文档来阐述这三者之间的关系. 1. Declaration Merging Declar ...
- typescript接口---interface
假如我现在需要批量生产一批对象,这些对象有相同的属性,并且对应属性值的数据类型一致.该怎么去做? 在ts中,因为要检验数据类型,所以必须对每个变量进行规范,自然也提供了一种批量规范的功能.这个功能就是 ...
- Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)
概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
TypeScript has 'interface' and 'type', so when to use which? interface hasName { firstName: string; ...
- React + TypeScript 默认 Props 的处理
React 中的默认 Props 通过组件的 defaultProps 属性可为其 Props 指定默认值. 以下示例来自 React 官方文档 - Default Prop Values: clas ...
- chrome插件: yapi 接口TypeScript代码生成器
前言 2020-09-12 天气晴,蓝天白云,微风,甚好. 前端Jser一枚,在公司的电脑前,浏览器打开着yapi的接口文档,那密密麻麻的接口数据,要一个一个的去敲打成为TypeScript的inte ...
- TypeScript 面试题汇总(2020 版)
TypeScript 面试题汇总(2020 版) TypeScript 3.9 https://www.typescriptlang.org/zh/ TypeScript 4.0 RC https:/ ...
随机推荐
- 观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别
观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别 在翻阅资料的时候,有人把观察者(Observer)模式等同于发布(Publish)/订阅(Subscribe ...
- flask + MySQL-python 创建 webapp 应用
0 - python 用自带的 wgsi 也可以创建 web 服务1)建立 hello.py 内容如下 # hello.pydef application(environ, start_respons ...
- 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)
A. Odd Palindrome 所有回文子串长度都是奇数等价于不存在长度为$2$的偶回文子串,即相邻两个字符都不同. #include<cstdio> #include<cstr ...
- 双接口(回调)promise cb
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [POJ1220]NUMBER BASE CONVERSION (高精,进制转换)
题意 任意进制之间的高进的转换 思路 相模倒排,高精处理 代码 我太弱了,下面附一个讨论里发的maigo思路的代码 ],A[]; ],d[]; main(){ for(scanf("%d&q ...
- Python练手例子(8)
43.模仿静态变量(static)另一案例. 程序分析:演示一个python作用域使用方法. #python3.7 class Num: nNum = 1 def inc(self): self.nN ...
- Jmeter响应数据中文乱码
在用Jmeter测试的时候吸纳供应数据如果出现中文乱码解决方法: 1.如下图在Content encoding输入框内输入 UTF-8
- CentOS最基本的20个常用命令
1. man 对你熟悉或不熟悉的命令提供帮助解释eg:man ls 就可以查看ls相关的用法注:按q键或者ctrl+c退出,在linux下可以使用ctrl+c终止当前程序运行. 2. ls 查看目录或 ...
- golang的包管理---vendor/dep等
首先关于vendor 1 提出问题 我们知道,一个工程稍大一点,通常会依赖各种各样的包.而Go使用统一的GOPATH管理依赖包,且每个包仅保留一个版本.而不同的依赖包由各自的版本工具独立管理,所以当所 ...
- js导航栏单击事件背景颜色变换
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...