好用的TypeScript

TS中的枚举定字符串

  • 通过给枚举定义字符串可以使用消息管理器
  • 之前都是用静态对象定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { EventEmitter } from 'events';

enum EventType {
OPEN = 'open',
CLOSE = 'close',
}

const eventEmitter = new EventEmitter();

eventEmitter.on(EventType.OPEN, () => {
console.log('call open');
});

eventEmitter.on(EventType.CLOSE, () => {
console.log('call close');
});

eventEmitter.emit(EventType.OPEN);
eventEmitter.emit(EventType.CLOSE);

TS中的单例模式

1
2
3
4
5
6
7
8
9
10
export class Singleton {
private static _instance: Singleton
private constructor() {}
static getInstance() : Singleton {
if (!Singleton._instance) {
Singleton._instance = new Singleton()
}
return Singleton._instance
}
}

TS中的可继承单例

  • 使用函数方法
1
2
3
4
5
6
7
8
9
10
11
12
13
export function Singleton<T>() {
class SingletonTemp {
private static _instance: any;
protected constructor() {}
static getInstance() : T {
if (!SingletonTemp._instance) {
SingletonTemp._instance = new this();
}
return SingletonTemp._instance as T;
}
}
return SingletonTemp;
}
  • 使用继承
1
2
3
4
5
class Test extends Singleton<Test>() {
print(msg: string) {
console.log(msg);
}
}
  • 测试
1
Test.getInstance().print("single")

使用type定义一个简单的pair

1
2
type pair<K, V> = [K, V];
const p: pair<number, string> = [1, 'name'];

TS中的Setter和getter实现

1
2
3
4
5
6
7
8
9
private _name: string

set name(val: string) {
this._name = val
}

get name() {
return this._name
}

TS函数模拟传入对象指针

  • 使用对象解构完成对象指针传入
  • 将当前对象的引用在函数做修改
1
2
3
4
5
6
7
8
9
10
11
12
13
let data = {
id: 1
}

function changeData({data: any} : any) {
let d = {
id: 2
}
data = d;
}
console.log('data before', data);
changeData({data});
console.log('data after',data);
  • 输出内容
1
2
data before { id: 1 }
data after { id: 2 }

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!