Problem with Retrieving Specific Data from Mixed Object
I'm encountering an issue with a function that is supposed to retrieve a specific piece of data within an object. The object contains a combination of known indexes and index signatures, which seems to be causing confusion for the function. I may have made a mistake in how I defined the function or perhaps there's a bug in the TypeScript linter. Any assistance on resolving this typing problem would be greatly appreciated.
The Function Definition
function getCounts<Entity extends EntityType>(entity: Entity, id: string): Counts[Entity][string] {
return counts[entity][id] ?? []; // triggers an error
}
Error Message Received
Type 'CountsWithEntity<Entity1> | CountsWithEntity<Entity2>' is not assignable to type 'Counts[Entity][string]'.
Type 'CountsWithEntity<Entity1>' is not assignable to type 'Counts[Entity][string]'.
Despite the Error, it Works!
Correct Inferred Type during Execution
Playground Link to Reproduce the Issue
Complete Code Snippet (same as above link)
[Code snippet provided]