概述

这是我学习typescript的笔记。写这个笔记的原因主要有2个,一个是熟悉相关的写法;另一个是理清其中一些晦涩的东西。供以后开发时参考,相信对其他人也有用。

学习typescript建议直接看中文文档英文文档。我是看的英文文档

typescript handbook 学习笔记2

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的更多相关文章

  1. typescript handbook 学习笔记4

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  2. typescript handbook 学习笔记2

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  3. typescript handbook 学习笔记1

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  4. typescript类(学习笔记非干货)

    我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...

  5. typescript接口(学习笔记非干货)

    typescript的核心原则之一就是对所具有的shape类型检查结构性子类型化 One of the core principles of typescript is to check struct ...

  6. 【TypeScript】学习笔记 把一些需要记的记录一下

    安装typescript: npm install -g typescript 启动typesctipt自动编译: tsc 文件名.ts --watch 函数参数默认值: 1.有默认值参数的,声明在最 ...

  7. typescript泛型(学习笔记非干货)

    软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型, 这在创建大型系统时为你提供了十分灵活的功能. In softwa ...

  8. TypeScript语言学习笔记(2)

    接口 // 在参数类型中定义约束 function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label ...

  9. TypeScript语言学习笔记(1)

    基本类型 // 布尔型(Boolean) let isDone: boolean = false; // 数值型(Number) let decimal: number = 6; let hex: n ...

随机推荐

  1. CSS定位方法

  2. vue踩坑(一):打包上线

    找到config→index.js 然后找到index.js的buildassetsPublicPath  这个修改为你的项目放在服务器的路径 像我的项目是放在wap 文件夹下的 这些配置完成后然后 ...

  3. 图像处理项目——生成csv文件提高读取效率

    利用pyhton脚本生成csv文件 *开发环境为windows PyCharm*使用的是pyhton脚本*生成人脸和人脸对应的标签的csv文件 一:主要步骤 1.载入对应路径2.提取每一张图片对应的位 ...

  4. Apache无法正常启动(配置多个监听端口)

    Apache监测多个端口配置: 1.conf->extra->httpd-vhosts.conf  检查配置项是否写错 2.http.conf listen端口是否监听正确 3.环境变量中 ...

  5. Laravel5 (cli)命令行执行脚本及定时任务

    Artisan是Laravel自带的命令行接口名称,它提供了很多有用的命令想要查看所有可用的Artisan命令,可使用list命令查看: 1 php artisan list 每个命令都可以用help ...

  6. 信号基础知识----线性调频信号LFM //matlab命令:chirp

    %关于线性调频信号(LFM) %参考书目:声呐技术,第二章P33 clc;close all;clear all;%参数----------------------------------f0=100 ...

  7. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call

    Error info: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, ...

  8. Python开发——文件操作

    文件的读取 http://www.cnblogs.com/linhaifeng/articles/5984922.html

  9. spring mvc+mybatis 构建 cms + 实现UC浏览器文章功能

    最近公司在模拟UC浏览器做一个简单的cms系统,主要针对于企业内部的文章浏览需求,这边考虑用户大多用mobile浏览文章内容,故使用原生的ios和android进行开发,后面也会集成html5. 1. ...

  10. Python select模块学习

    select 是常用的异步socket 处理方法 一般用法: # iwtd,owtd,ewtd 分别为需要异步处理的读socket队列, 写socket队列(一般不用), 和错误socket队列, 返 ...