TypeScript中的泛型、枚举及相关特性深入解析
1. 泛型在类中的应用
泛型可以在类定义中使用,以下是一个简单的示例:
class NaiveMap<Key, Value> {
private _keys: Key[] = [];
private _values: Value[] = [];
constructor(){}
contains(key: Key): boolean {
const result = this._keys.indexOf(key);
return result !== -1;
}
put(key: Key, value: Value): void {
if(!this.contains(key)) {
this._keys.push(key);
this._values.push(value);
}
}
get(key: Key): Value | undefined {
if(this.contains(key)) {
return this._values[this._keys.indexOf(key)];
} else{
return undefined;
}
}
}
class Thing {
constructor(public name: string){}
}
const naiveMap
超级会员免费看
订阅专栏 解锁全文
4005

被折叠的 条评论
为什么被折叠?



