typescript handbook 学习笔记3
概述
这是我学习typescript的笔记。写这个笔记的原因主要有2个,一个是熟悉相关的写法;另一个是理清其中一些晦涩的东西。供以后开发时参考,相信对其他人也有用。
学习typescript建议直接看中文文档或英文文档。我是看的英文文档。
interfaces接口
类接口
interface SquareConfig {
//标准写法
label: string;
//可选属性
color?: string;
//只读属性
readonly x: number;
//缺省属性
[propName: string]: any;
}
//使用接口
function createSquare(config: SquareConfig): {color: string; area: number} {
//用config.color这个形式调用
}
//跳过额外属性检查的方法一:类型断言(强制跳过)
let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig);
//跳过额外属性检查的方法二:赋给变量(自动跳过)
let squareOptions = { colour: "red", width: 100 };
let mySquare = createSquare(squareOptions);
其它接口
//函数接口,参数名不重要
interface SearchFunc {
(a: string, b: string): boolean;
}
//使用函数接口,注意参数中的: string可以省略。
let mySearch: SearchFunc;
mySearch = function(source: string, substring: string): boolean {
let result = source.search(substring);
return result > -1;
}
//可索引的接口(数字索引)
interface StringArray {
[index: number]: string;
}
//使用可索引接口
let myArray: StringArray;
myArray = ['Bob', 'Fred'];
let myStr: string = myArray[0];
//使用数字索引+字符串索引时,数字索引的类型需要是字符串索引的类型的子类型
iterface NumberDictionary {
[index: string]: number; //字符串索引
readonly [index: number]: number; //只读数字索引,必须为number的子类型
length: number; //ok
name: string; //error
}
实现接口
接口只会检查类的实例属性,不会检查类的静态属性。所以不会检查constructor。如果constructor要用接口检查的话,需要额外给它定义一个接口,如下所示:
//额外定义constructor接口
interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
tick();
}
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
return new ctor(hour, minute);
}
class DigitalClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("beep beep");
}
}
class AnalogClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("tick tock");
}
}
let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);
继承接口
interface Shape {
color: string;
}
interface PenStroke {
penWidth: number;
}
//继承用extends,实现类用implements。
interface Square extends Shape, PenStroke {
sideLength: number;
}
//接口的另一种写法。实现一个对象。为什么不用:?
let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
混合接口
//既可以当做函数接口,又可以当做类接口
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
//当做函数接口
let counter01 = <Counter>function(start: number) {};
//当做类接口
let counter02 = <Counter>{};
counter02.interval = 2;
counter02.reset();
继承类的接口
class Control {
private state: any;
}
interface SelectableControl extends Control {
select(): void;
}
//error,需要先继承Control才能实现接口SelectableControl
class Image implements SelectableControl {
select() { }
}
//OK
class Button extends Control implements SelectableControl {
select() { }
}
typescript handbook 学习笔记3的更多相关文章
- typescript handbook 学习笔记4
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript handbook 学习笔记2
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript handbook 学习笔记1
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript类(学习笔记非干货)
我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...
- typescript接口(学习笔记非干货)
typescript的核心原则之一就是对所具有的shape类型检查结构性子类型化 One of the core principles of typescript is to check struct ...
- 【TypeScript】学习笔记 把一些需要记的记录一下
安装typescript: npm install -g typescript 启动typesctipt自动编译: tsc 文件名.ts --watch 函数参数默认值: 1.有默认值参数的,声明在最 ...
- typescript泛型(学习笔记非干货)
软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型, 这在创建大型系统时为你提供了十分灵活的功能. In softwa ...
- TypeScript语言学习笔记(2)
接口 // 在参数类型中定义约束 function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label ...
- TypeScript语言学习笔记(1)
基本类型 // 布尔型(Boolean) let isDone: boolean = false; // 数值型(Number) let decimal: number = 6; let hex: n ...
随机推荐
- css:元素水平垂直居中的多种方式
CSS元素(文本.图片)水平垂直居中方法 1.text-align:center; 2.margin:0 auto; 3.display:inline-block; + text-align:ce ...
- (译)内存沉思:多个名称相关的神秘的SQL Server内存消耗者。
原文出处:https://blogs.msdn.microsoft.com/sqlmeditation/2013/01/01/memory-meditation-the-mysterious-sql- ...
- cmake 指定连接的opencv版本
我们通常需要使用不同版本的opencv,这时候如果用cmake构建工程,直接指定本地编译好的路径就可以,在CMakeLists.txt中添加: set(OpenCV_DIR "/xxx/wo ...
- oracle用户间表数据复制迁移
如system用户要将scott中的emp表倒入其中,按如下方法: 1.登录scott用户 2.给system用户赋予查询emp标的权限: grant select on emp to system; ...
- Python学习—基础篇之文件操作
文件操作 文件操作也是编程中需要熟练掌握的技能,尤其是在后台接口编写和数据分析过程中,对各种类型的文件进行操作,获取文件信息或者对信息进行存储是十分重要的.本篇博客中将主要对常见的文本格式文件和Exc ...
- linux 部分常用命令
1.Linux 删除除了某个文件之外的所有文件 [root@localhost abc]# ls |grep -v 'a' |xargs rm -f 其中rm -f !(a) 最为方便.如果保留a和 ...
- document.getElementById(“id”)与$("#id")的区别
document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...
- 大数据学习笔记2 - 分布式文件系统HDFS(待续)
分布式文件系统结构 分布式文件系统是一种通过网络实现文件在多台主机上进行分布式存储的文件系统,采用C/S模式实现文件系统数据访问,目前广泛应用的分布式文件系统主要包括GFS和HDFS,后者是前者的开源 ...
- java得到当前日期的前一天或后一天
public String getNextDay(String startdate) throws ParseException{ Date date = (new SimpleDateFormat( ...
- android 隐藏虚拟按钮栏及标题等权限设置
华为手机有虚拟按钮,根据以下设置方法可以进行隐藏控制 /** * 隐藏虚拟按键,并且全屏 */ protected void hideBottomUIMenu(Context context){ if ...