I am working on the code below:
type MyDict = {
[k: string]: unknown;
};
function fn<T extends MyDict>(items: T) {
const functionDict: { [k: string]: (val: keyof T) => void } = Object.fromEntries(
Object.keys(items).map((key: keyof T) => [
key,
(val: keyof T) => console.log(val),
]),
);
}
Is there a way to explicitly declare functionDict
as an object (or a Record<>
) with keys that are specifically key of T
instead of the generic type string
?
For example, if I use
type MyExtendedDict = MyDict & {
foo: number;
bar: number;
}
fn<MyExtendedDict>({
foo: 1,
bar: 2
})
I want to clearly specify that the dictionary's keys should be "foo" and "bar".