1 2 3 4
|
function foo(name: string, gender: string = 'female, age?: number): void { }
|
1 2 3 4 5 6 7 8 9 10 11
|
function add(a: number, b: number): number; function add(a: string, b: string): string; function add(a: string, b: number): string; function add(a: number, b: string): string; function add(a: Combinable, b: Combinable) { if (typeof a === 'string' || typeof b === 'string') { return a.toString() + b.toString(); } return a + b; }
|
private 与# 的区别
私有变量可以使用 getter 和 setter 方法来访问或者修改私有变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
let passcode = "Hello TypeScript";
class Employee { private _fullName: string;
get fullName(): string { return this._fullName; }
set fullName(newName: string) { if (passcode && passcode == "Hello TypeScript") { this._fullName = newName; } else { console.log("Error: Unauthorized update of employee!"); } } }
let employee = new Employee(); employee.fullName = "Semlinker"; if (employee.fullName) { console.log(employee.fullName); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class ProductService { getProducts(): void; getProducts(id: number): void; getProducts(id?: number) { if(typeof id === 'number') { console.log(`获取id为 ${id} 的产品信息`); } else { console.log(`获取所有的产品信息`); } } }
const productService = new ProductService(); productService.getProducts(666); productService.getProducts();
|
-
装饰器参考链接
-
1.
TypeScript笔记
-
1.1.
基础类型
-
1.1.1.
unknown类型
-
1.1.2.
tuple元祖
-
1.1.3.
object,Object,{}的区别
-
1.1.3.1.
object是一个数据类型,可用于声明变量,但是微软现在不建议这样使用,而是建议使用Record<string, unknown>
-
1.1.3.2.
Object 是所有 Object 类的实例类型,与JS的Object使用类似
-
1.1.3.3.
{} 是一个没有成员的对象,是一个具体的对象数据,不是一个类型
-
1.1.4.
Never 类型
-
1.2.
TypeScript 断言
-
1.3.
交叉类型
-
1.4.
TypeScript 函数
-
1.4.1.
与 JavaScript 的区别
-
1.4.1.1.
ts 函数参数需要声明类型,函数的返回值类型也需要声明,可选参数也需要声明
-
1.4.2.
有了严格的方法参数定义后,TS 就有了函数重载
-
1.5.
TypeScript 接口
-
1.5.1.
interface 与 type 的区别
-
1.5.2.
继承extends与实现Implements
-
1.5.2.1.
extends
-
1.5.2.2.
Implements
-
1.6.
TypeScript 的面向对象
-
1.6.1.
11.2 ECMAScript 私有字段
-
1.6.2.
TypeScript 支持抽象 abstract
-
1.6.3.
TypeScript 类方法支持重载
-
1.7.
泛型语法
-
1.8.
装饰器
-
1.8.1.
装饰器是什么?
-
1.8.2.
装饰器分类
-
1.8.3.
类装饰器
-
1.8.4.
属性装饰器
-
1.8.5.
方法装饰器
-
1.8.6.
方法参数装饰器
-
1.8.7.
装饰器的执行顺序