I have a function logNumbers that accepts a dictionary where all keys are strings and all values are numbers. I want to call logNumbers with an object belongs to a stricter interface but still satisfies those conditions.
interface NumberDictionary {
[key: string]: number;
}
interface StatisticsResult {
mean: number;
mode: number;
median: number;
}
function logNumbers(numDict: NumberDictionary) { ... }
const results: StatisticsResult = {
mean: 1,
mode: 2,
median: 2,
};
//
// Error:
// Argument of type 'StatisticsResult' is not assignable to parameter of type 'NumberDictionary'.
// Index signature is missing in type 'StatisticsResult'.
//
logNumbers(results);
I would like for StatisticsResult to remain the same and to somehow modify the signature for logNumbers. Is there a way to do this? Perhaps I could signal to typescript that no new keys will be added to numDict within logNumbers?