I am trying to create to type my class with generics and it alays throws me an error
class CustomDataStructure<T> {
public _data: object;
constructor() {
this._data = {};
}
public getAndDeleteRandomKey(): void {
const allExistingKeys = Object.keys(this._data);
const randomKey = allExistingKeys[Math.floor(Math.random()*(allExistingKeys.length))];
this.removeItem(randomKey);
}
public addItem(value: T): void {
console.log("adding");
console.log(this._data)
this._data[value] = new Date().getTime();
}
public removeItem(key: T): void {
delete this._data[key];
}
}
let ss = new CustomDataStructure<string>();
ss.addItem("hello");
ss.addItem("hello2");
This throws me 2 different errors
parameter of type 'T'.'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
Type 'T' cannot be used to index type 'object'
am i missing anything here