Type interface of map error

interface TableData {
[databaseName: string]: number;
}
function foo(): TableData {
let tableData = new Map<TableData>();
// No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.

tableData.set('database1', 123);
return tableData
}
interface TableData {
[databaseName: string]: number;
}
function foo(): TableData {
let tableData = new Map<TableData>();
// No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.

tableData.set('database1', 123);
return tableData
}
Why does this not work? I have been googling for over 40 minutes but I don't get it. I know I could do something like new Map<string, number>() but why not by using the interface? Thanks!
3 Replies
erik.gh
erik.gh16mo ago
what you want is let tableData = new Map<string, number>();
Brendonovich
Brendonovich16mo ago
new Map<string, number>() and new Map<TableData>() are fundamentally different things. The first generic argument (string and TableData) is for the map's key, and the second number is for the map's values. You can't just merge the two generics together into an interface, you have to provide them separately - putting TableData first is telling TS that they key type is TableData, but you're not providing a value type, hence the complaint about 0 or 2 arguments.
erik.gh
erik.gh16mo ago
if you really want to have an interface you can split it like this let tableData = new Map<keyof TableData, TableData[string]>();