Having had experience working with C# for a while, I recently ventured into a Node.js project using TypeScript V3.1.6. It was exciting to discover that TypeScript now supports generics, something I thought I would miss from my C# days.
In my C# code, I had a DataRow wrapper that allowed me to extract strongly typed values from a Dictionary using generics:
Dictionary<string, object> Values = new Dictionary<string, object>();
public T Parse<T>(string columnName)
{
T result = default(T);
result = (T)Convert.ChangeType(this.Values[columnName], typeof(T));
return result;
}
My current TypeScript code looks like this:
export class DataRow {
private Values: Map<string, object> = new Map<string, object>();
constructor(row?: any) {
if (!!row) {
for (let key in row) {
this.Values.set(key, row[key]);
}
}
}
public Value<T>(key: string): T {
//Not sure how to handle the return here.
}
}
... (additional content remains unchanged)