'Typescript - Expression is not callable but fixed by a guard/narrowing

Here is my function.

export class GetPipe {
    public transform<K, V>(
        list:
            | Array<V>
            | List<V>
            | Map<K, V>
            | ImmutableMap<K, V>,
        id: any,
    ): V | undefined {
        if (Array.isArray(list)) {
            return list[id];
        }

        // remove the comments this to remove the error
        // if (List.isList(list)) {
        //     return list.get(id);
        // }
        return list.get(id);
    }
}

tsc returns an error for the last call list.get(id).

Any idea why I do need to use that guard to fix the issue ?

Demo on Stackblitz



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source