Typescript can't cast abstract function array, results in error, why?

Viewed 98

Typescript throws an error for this code(abstracted- not the real code) :

export const enum typeList {
    a = "aa",
    b = "bb",
    c = "cc"
}

export interface parameterEvent<S extends typeList> {
    "kind": S;
    "detail": DetailTypes[S];
}

export interface DetailTypes {
    [typeList.a]: string;
    [typeList.b]: boolean;
    [typeList.c]: undefined;
}

interface returnTypes {
    [typeList.a]: number | string;
    [typeList.b]: number;
    [typeList.c]: boolean;
}

type Universial<T extends typeList> = (event: parameterEvent<T>) => returnTypes[T];

type collection = {
    [T in typeList]: Universial<T>[];
};

const universialGroup: collection = {
    [typeList.a]: [],
    [typeList.b]: [],
    [typeList.c]: []
};

export function addToUniversalGroup<T extends typeList>(kind: T, val: Universial<T>): void {
    const listeners = universialGroup[kind] as Universial<T>[];
}

Error:

Conversion of type 'collection[T]' to type 'Universial[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. Type 'Universial<typeList.a>[] | Universial<typeList.b>[] | Universial<typeList.c>[]' is not comparable to type 'Universial[]'. Type 'Universial<typeList.c>[]' is not comparable to type 'Universial[]'. Type 'Universial<typeList.c>' is not comparable to type 'Universial'. Type 'boolean' is not comparable to type 'returnTypes[T]'.

This example on TypescriptLang

One interesting fact is that removing event: parameterEvent<T> from the Universal type fixes this error which doesn't make sense IMHO.

I'm trying to understand what is wrong with this example? Why do I get this error message?

0 Answers
Related