一、基础类型/变量声明/接口/class
Created: May 10, 2021 9:34 AM
一、基础类型
数字,字符串,结构体,布尔值等
1.基本类型
- 布尔值
let isDone: boolean = false;
- 布尔值
2.数字
和JavaScript一样,TypeScript里的所有数字都是浮点数。 这些浮点数的类型是number.tsx let decLiteral: number = 6;3.字符串
string表示文本数据类型typescript let sentence: string = `Hello, my name is ${ name }.4.数组
tsx // 第一种,可以在元素类型后面接上[] let list: number[] = [1, 2, 3]; // 第二种方式是使用数组泛型,Array<元素类型>: let list: Array<number> = [1, 2, 3];5.元组 Tuple
元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同tsx // Declare a tuple type let x: [string, number]; // 当访问一个越界的元素,会使用联合类型替代: x[3] = 'world'; // OK, 字符串可以赋值给(string | number)类型6.枚举
enum类型是对JavaScript标准数据类型的一个补充.
默认情况下,从0开始为元素编号。 你也可以手动的指定成员的数值。 例如,我们将上面的例子改成从1开始编号tsx enum Color {Red, Green, Blue} let c: Color = Color.Green;7.任意值
我们不希望类型检查器对这些值进行检查而是直接让它们通过编译阶段的检查。
当你只知道一部分数据的类型时,any类型也是有用的。 比如,你有一个数组,它包含了不同的类型的数据:tsx let list: any[] = [1, true, "free"];8.空值
void类型像是与any类型相反,它表示没有任何类型。 当一个函数没有返回值时.
声明一个void类型的变量没有什么大用,因为你只能为它赋予undefined和nulltsx let unusable: void = undefined;9.Null 和 Undefined
默认情况下null和undefined是所有类型的子类型。 就是说你可以把null和undefined赋值给number类型的变量。
当你指定了—strictNullChecks标记,null和undefined只能赋值给void和它们各自。 这能避免很多常见的问题> 注意:我们鼓励尽可能地使用—strictNullChecks,但在本手册里我们假设这个标记是关闭的。
- 10.Never
never类型表示的是那些永不存在的值的类型。 - 11.symbol
Symbols是不可改变且唯一的
let sym2 = Symbol("key");
2.类型断言
有时候你会遇到这样的情况,你会比TypeScript更了解某个值的详细信息
// 其一是“尖括号”语法:let someValue: any = "this is a string";let strLength: number = (<string>someValue).length;// 另一个为as语法let strLength: number = (someValue as string).length;
二、变量声明
let和const是JavaScript里相对较新的变量声明方式。 像我们之前提到过的, let在很多方面与var是相似的,但是可以帮助大家避免在JavaScript里常见一些问题。 const是对let的一个增强,它能阻止对一个变量再次赋值。
1. var 声明
标识符提升【变量提升】:它允许变量不声明就可以访问
捕获变量怪异之处:
for (var i = 0; i < 10; i++) {setTimeout(function() { console.log(i); }, 100 * i);}// 10 10·······10我们传给setTimeout的每一个函数表达式实际上都引用了相同作用域里的同一个i。
作用域规则
var声明可以在包含它的函数,模块,命名空间或全局作用域内部任何位置被访问(我们后面会详细介绍),包含它的代码块对此没有什么影响。 有些人称此为var作用域或函数作用域。 函数参数也使用函数作用域。
2. let 声明
块作用域:
- 当用let声明一个变量,它使用的是词法作用域或块作用域。 不同于使用 var声明的变量那样可以在包含它们的函数外访问,块作用域变量在包含它们的块或for循环之外是不能访问的。
- 拥有块级作用域的变量的另一个特点是,它们不能在被声明之前读或写
重定义及屏蔽
- 块级作用域变量不能用函数作用域变量来声明。 而是块级作用域变量需要在明显不同的块里声明。
- 在一个嵌套作用域里引入一个新名字的行为称做屏蔽
块级作用域变量的获取
当let声明出现在循环体里时拥有完全不同的行为。 不仅是在循环里引入了一个新的变量环境,而是针对 每次迭代都会创建这样一个新作用域。
for (let i = 0; i < 10 ; i++) {setTimeout(function() {console.log(i); }, 100 * i);}// 0,1,2,3····9
3. const 声明
它们与let声明相似,但是就像它的名字所表达的,它们被赋值后不能再改变
4. let vs. const
使用最小特权原则,所有变量除了你计划去修改的都应该使用const。 基本原则就是如果一个变量不需要对它写入,那么其它使用这些代码的人也不能够写入它们,并且要思考为什么会需要对这些变量重新赋值。
5.解构
解构数组
let input = [1, 2];let [first, second] = input; // 1, 2let [first, ...rest] = [1, 2, 3, 4];console.log(first); // outputs 1
对象解构
let o = {a: "foo",b: 12,c: "bar"};let { a, b } = o;
属性重命名
let { a: newName1, b: newName2 } = o;
默认值
let { a, b = 1001 } = wholeObject;
展开
let first = [1, 2];let second = [3, 4];let bothPlus = [0, ...first, ...second, 5];
三、接口
TypeScript的核心原则之一是对值所具有的结构进行类型检查。 它有时被称做“鸭式辨型法”或“结构性子类型化”。
interface SquareConfig {// 基础color: string;// 可选属性width?: number;// 只读属性.一些对象属性只能在对象刚刚创建的时候修改其值readonly x: number;//let p1: Point = { x: 10, y: 20 };// p1.x = 5; // error!// 额外的属性检查//最佳的方式是能够添加一个字符串索引签名,前提是你能够确定这个对象可能具有某些做为特殊用途使用的额外属性。[propName: string]: any;// 例子:// 只要它们不是color和width,那么就无所谓它们的类型是什么interface SquareConfig {color?: string;width?: number;[propName: string]: any;}}// 函数类型interface SearchFunc {(source: string, subString: string): boolean;}//可索引的类型interface StringArray {[index: number]: string;// 这个索引签名表示了当用number去索引StringArray时会得到string类型的返回值。// 共有支持两种索引签名:字符串和数字。// 但是数字索引的返回值必须是字符串索引返回值类型的子类型。 这是因为当使用number来索引时,JavaScript会将它转换成string然后再去索引对象。}let myArray: StringArray;myArray = ["Bob", "Fred"];// 类类型interface ClockInterface {currentTime: Date;setTime(d: Date);}// 继承接口interface Shape {color: string;}interface PenStroke {penWidth: number;}interface Square extends Shape, PenStroke {sideLength: number;}
readonly vs const
最简单判断该用readonly还是const的方法是看要把它做为变量使用还是做为一个属性。 做为变量使用的话用 const,若做为属性则使用readonly。
混合类型
因为JavaScript其动态灵活的特点,有时你会希望一个对象可以同时具有上面提到的多种类型。
interface Counter {(start: number): string;interval: number;reset(): void;}function getCounter(): Counter {let counter = <Counter>function (start: number) { };counter.interval = 123;counter.reset = function () { };return counter;}let c = getCounter();c(10);c.reset();c.interval = 5.0;
接口继承类
当接口继承了一个类类型时,它会继承类的成员但不包括其实现。
四、类
1. 类
class Greeter {greeting: string; // 属性// 构造函数constructor(message: string) {this.greeting = message;}// 方法greet() {return "Hello, " + this.greeting;}}
2.继承
class Animal {name: string;constructor(theName: string) { this.name = theName; }move(distanceInMeters: number = 0) {console.log(`${this.name} moved ${distanceInMeters}m.`);}}class Snake extends Animal {constructor(name: string) { super(name); }move(distanceInMeters = 5) {console.log("Slithering...");super.move(distanceInMeters);}}class Horse extends Animal {constructor(name: string) { super(name); }move(distanceInMeters = 45) {console.log("Galloping...");super.move(distanceInMeters);}}let sam = new Snake("Sammy the Python");let tom: Animal = new Horse("Tommy the Palomino");sam.move();tom.move(34);
3.公共,私有与受保护的修饰符
默认为public
理解private
class Animal {private name: string;constructor(theName: string) { this.name = theName; }}new Animal("Cat").name; // 错误: 'name' 是私有的.
理解protected
protected成员在派生类中仍然可以访问
构造函数也可以被标记成protected。 这意味着这个类不能在包含它的类外被实例化,但是能被继承.
class Person {protected name: string;protected constructor(theName: string) { this.name = theName; }}// Employee 能够继承 Personclass Employee extends Person {private department: string;constructor(name: string, department: string) {super(name);this.department = department;}public getElevatorPitch() {return `Hello, my name is ${this.name} and I work in ${this.department}.`;}}// 是person的派生类constructorlet howard = new Employee("Howard", "Sales");// constructor 被protected修饰,因此不能被实例化let john = new Person("John"); // 错误: 'Person' 的构造函数是被保护的.
4. readonly修饰符
你可以使用readonly关键字将属性设置为只读的。 只读属性必须在声明时或构造函数里被初始化。
5.存取器
TypeScript支持通过getters/setters来截取对对象成员的访问。 它能帮助你有效的控制对对象成员的访问。
let passcode = "secret passcode";class Employee {private _fullName: string;get fullName(): string {return this._fullName;}set fullName(newName: string) {if (passcode && passcode == "secret passcode") {this._fullName = newName;}else {console.log("Error: Unauthorized update of employee!");}}}let employee = new Employee();employee.fullName = "Bob Smith";if (employee.fullName) {alert(employee.fullName);}
