admin管理员组文章数量:1405393
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?
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?
Share Improve this question edited Mar 22 at 12:11 DrawMen asked Mar 22 at 11:49 DrawMenDrawMen 408 bronze badges 2- could you provide the full code with ITableColumn? – Alexander Nenashev Commented Mar 22 at 12:08
- yes, I have added it to my code sample – DrawMen Commented Mar 22 at 12:11
1 Answer
Reset to default 2Why just not to map by the index?:
The related documentation
Playground
type MapToColumns<T extends unknown[]> = {[I in keyof T]: ITableColumn<T[I]>};
本文标签: typescriptMapped generic array type missing propertiesStack Overflow
版权声明:本文标题:typescript - Mapped generic array type missing properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744316501a2600289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论