actionsCollection has union type with similar structure:
{
install: {...}
bid: {...}
} | {
travel: {...}
bid: {...}
pass: {...}
} | ...
None of the keys in subsets are mandatory or unique.
Here's my code:
if (
!(actionType in actionsCollection) ||
('install' in actionsCollection) || // this check has an effect
!Object.prototype.hasOwnProperty.call(
actionsCollection,
actionType
) ||
!Object.keys(actionsCollection).includes(actionType) ||
!Object.hasOwn(actionsCollection, actionType)
) {
return socket!.emit('socketError', ERRORMESSAGES.UNKNOWNACTION);
}
let ttt = actionsCollection.install; // if I return when ('install' in actionsCollection) is truthful, this line throws an error
ttt = actionsCollection[actionType]; // this line always throws an error
The error says:
Element implicitly has an 'any' type because expression of type '"install" | "bid" | "travel" | "pass" | "place" | "concede" | "playcard"' can't be used to index type '{ install: (p: DefaultActionParams & { polynominoId: number; polynominoOrientation: number; polynominoPosition: number; }) => Promise<ProcessPlayerActionReturn>; bid: (p: DefaultActionParams & { ...; }) => Promise<...>; travel: (p: DefaultActionParams & { ...; }) => Promise<...>; pass: (p: DefaultActionParams & { .....'.
Property 'place' does not exist on type '{ install: (p: DefaultActionParams & { polynominoId: number; polynominoOrientation: number; polynominoPosition: number; }) => Promise<ProcessPlayerActionReturn>; bid: (p: DefaultActionParams & { ...; }) => Promise<...>; travel: (p: DefaultActionParams & { ...; }) => Promise<...>; pass: (p: DefaultActionParams & { .....'.ts
In other words, when I check for a presence of a particular string ('install') key, TypeScript reacts. But how do I let TypeScript know, that actionsCollection can be cast to a certain subset of a union, or at least has actionsCollection as a key?