I encountered the following issue:
An error occurred stating that Type 'KeyValuePair<string, Date>' is not assignable to type 'KeyValuePair<number, string>'.
Also, it mentioned that Type 'string' is not assignable to type 'number'.
This problem is related to the code snippet below:
class KeyValuePair<TKey, TValue> {
constructor(public key: TKey, public value: TValue) {}
}
class KeyValuePairPrinter<T, U> {
constructor(private pairs: KeyValuePair<T, U>[]) {}
print() {
for (let p of this.pairs) {
console.log(`${p.key}: ${p.value}`);
}
}
}
let now = new Date(Date.now());
let pair1 = new KeyValuePair<number, string>(1, "First");
let pair2 = new KeyValuePair<string, Date>("Second", new Date(Date.now()));
let pair3 = new KeyValuePair<number, string>(3, "Third");
var printer = new KeyValuePairPrinter([
pair1,
pair2,
pair3,
]);
printer.print();
You can view the screenshot for reference here: https://i.stack.imgur.com/Md2LQ.png
What do you think might be causing this error?