xxxxxxxxxx
const genericObject: { [key:string]: any } = {
arrayOfNumbers: [1, 2, 3],
emptyObject: {},
stringProp: 'hello'
}
xxxxxxxxxx
// Creating a generic object in TypeScript
class MyGenericObject<T> {
private value: T;
constructor(val: T) {
this.value = val;
}
getValue(): T {
return this.value;
}
}
// Usage of the generic object
const myObj = new MyGenericObject<number>(42);
console.log(myObj.getValue()); // Output: 42
const anotherObj = new MyGenericObject<string>("Hello, World!");
console.log(anotherObj.getValue()); // Output: Hello, World!