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

134 views Asked by At

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

0

There are 0 answers