My aim with TypeScript is to generate an Array containing different generics.
The purpose of my generic class is to define table columns, with T representing the type of values the column will contain. The structure is like this:
export class TableColumnConfig<T> { ... }
When adding multiple columns to a table, I follow these steps:
const columns: TableColumnConfig<any>[] = [];
Subsequently, I do something along the lines of:
columns.push(new TableColumnConfig<number>());
columns.push(new TableColumnConfig<string>());
This implementation works effectively, but there's a note from eslint advising against the use of any
Unexpected any. Specify a different type.eslint@typescript-eslint/no-explicit-any
Eslint recommends using unknown.
To adhere to this recommendation, I make the adjustment here:
const columns: TableColumnConfig<unknown>[] = [];
However, this leads to the following error:
Argument of type 'TableColumnConfig<number>' is not assignable to parameter of type 'TableColumnConfig<unknown>'. Type 'unknown' is not assignable to type 'number'.ts(2345)
Is there a solution that satisfies both eslint and avoids TypeScript syntax errors?
Would it be acceptable in this scenario to ignore eslin'ts warning?
Your insights and guidance are greatly appreciated! :)
EDIT: This is how my class and the builder that I use for it looks like:
export class TableColumnConfig<T> {
// lots of properties (that don't utilize T)
format?: (val: T) => string;
sort?: (val1: T, val2: T) => number;
constructor() {
// ...
}
}
export class TableColumnConfigBuilder<T> implements ITableColumnConfigBuilder<T> {
private tableColumnConfig: TableColumnConfig<T>;
constructor() /* some mandatory properties */
{
this.tableColumnConfig = new TableColumnConfig(
sourceAddress,
parameterAddress,
dataType,
mainLabel
);
}
// ...
setFormat(format: (val: T) => string): this {
this.tableColumnConfig.format = format;
return this;
}
setSort(sort: (val1: T, val2: T) => number): this {
this.tableColumnConfig.sort = sort;
return this;
}
get(): TableColumnConfig<T> {
return this.tableColumnConfig;
}
}
interface ITableColumnConfigBuilder<T> {
// ...
setFormat(format: (val: T) => string): this;
setSort(sort: (val1: T, val2: T) => number): this;
}