I want a "toList" function on the generic record type. Is there a way to achieve something like this? Edit: It must not be a solution with the record type. I am just searching for something similar, where I can still use myDictionary["key"].
//this is possible
type MyRecord<K extends string, T> = Record<K, T> & {
toList(this: MyRecord<K, T>): T[];
}
//this not. Typescript complains here about using MyRecord as value when it is a type
MyRecord.prototype.toList = function():T[] {
const list: T[] = [];
...
return list;
};
Where I can use it later like this without having to define a toList function on every assignment.
//this won't work, toList is missing;
const myDictionary: MyRecord<string, string> = { "a": "b", "c": "d" }
const myDictionaryValues = myDictionary.toList();
myDictionaryValues.forEach(v => console.log(v));
//prints:
//b
//d
Neither the MyRecord.prototype is working nor the declaration in the first line of my use case example (because of the missing "toList").
I know that it is possible to just have a global function somewhere, which is doing the "toList" thing. That is not what I am asking here.