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 ...
随机推荐
- SQL Server 中的6种事务隔离级别简单总结
本文出处:http://www.cnblogs.com/wy123/p/7218316.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...
- Hadoop 学习之Docker
docker环境的配置请参考:http://www.cnblogs.com/frankliu/p/8052673.html hadoop-docker安装地址参考:https://hub.docker ...
- Spring再接触 IOC DI
直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...
- netty 为什么用nio 不用 aio
NIO模型 同步非阻塞 NIO有同步阻塞和同步非阻塞两种模式,一般讲的是同步非阻塞,服务器实现模式为一个请求一个线程,但客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才 ...
- spfa与dijkstra(最短路)
spfa: void spfa(){ queue<int> que; while(!que.empty()){que.pop();} que.push(s); vis[s]=; while ...
- PowerScript语言基础
注释: 以 "//" 开头,其后书写注释内容,常用于单行注释. "/-/"中间的部分为注释,便于多行说明. //这是一个单行注释 INTEGER I I = I ...
- (转)C# Windows服务 弹出消息提醒框
出处:http://blog.csdn.net/donghui6116773/article/details/53467069 服务(Service)对于大家来说一定不会陌生,它是Windows 操作 ...
- 受欢迎的牛[HAOI2006]
--BZOJ1051 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎, ...
- 201621123002《Java程序设计》第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...
- 20165213周启航java学习总结
20165213周启航java学习总结 一.每周博客链接及二维码 预备作业一:我所期望的师生关系:http://www.cnblogs.com/rocedu/p/6255835.html#WEEK15 ...