I am working with a `packages` object where I add items of the `Package` type ( See my code here and also Playground link), like this:
type Callback = (obj: {
source: string,
types: string[],
meta?: {}
}) => void;
interface Package {
callback: Callback;
}
const packages: { [packageId: string]: Package } = {};
const addPackage = (
packageId: string,
callback: Callback
) => {
packages[packageId] = { callback };
};
When I start typing `addPackage('somePackage'`, TypeScript will correctly infer that `packageId` is of type `string`, but it can only determine that `callback` is of type `Callback`:
https://i.sstatic.net/k0jYu.png
However, if I type it out as a function, TypeScript shows me the fields of `callback`:
https://i.sstatic.net/HUyXc.png
But this information is not displayed unless I know that `callback` should be a function with specific keys as its argument.
How can I communicate to someone using `addPackage` that `callback` needs to be a function with the fields of `Callback`?
The structure of my object (in plain JavaScript) should look like this:
{
123: {
callback: () => {
// The callback function added by `addPackage`.
// Access to the parameters 'source' and 'types' is available here.
}
}
}
As an additional note, TypeScript fails to highlight `callbacks` properties when I try to access them. They are no longer visible:
https://i.sstatic.net/vj1BI.png
I welcome constructive criticism as I continue to learn TypeScript. If you have a better approach to solving this issue, please share as I value your input greatly.