typescripttypescript-genericsmapped-types

Mapped generic array type missing properties


I'm trying to create a GenericTable class that has a property based on the generic type that I'm giving it.

Currently I have this:

interface ITableColumn<C> {
    name: string;
    isUnsortable?: boolean;
    sortAdapter?: (a: C, b: C) => number;
    template?: TemplateRef<{ $implicit: C }>;
    width?: number;
}

//Map array of types to ITableColumn[] of the same types
type MapToColumns<
    T extends unknown[],
    Acc extends readonly ITableColumn<unknown>[] = [],
> = T['length'] extends Acc['length'] ? Acc : MapToColumns<T, readonly [...Acc, ITableColumn<T[Acc['length']]>]>;

class GenericTable<T extends unknown[]> {
    public constructor(private _columns: MapToColumns<T>) {
        const index = _columns.findIndex(() => ...)
    }
}

My issue is that 'findIndex' doesn't exists anymore on the _columns property. All works fine when I'm not using a generic but eg: [string, number, string] instead. Anyone see what is going wrong here, or know of any better approaches?


Solution

  • Why just not to map by the index?:

    The related documentation

    Playground

    type MapToColumns<T extends unknown[]> = {[I in keyof T]: ITableColumn<T[I]>};