export abstract class GridColumn {
public field?: string;
public sortField?: string;
public header?: string;
public footer?: string;
public sortable?: any = true;
public editable?: boolean = false;
public filter?: boolean = true;
public filterMatchMode?: string = 'contains';
public filterPlaceholder?: string;
public style?: any;
public styleClass?: string;
public hidden?: boolean = false;
public selectionMode?: string = 'multiple';
public frozen?: boolean;
}
For instance, by doing this, you will only receive an object with those specific properties.
private gridConf: GridColumn = <GridColumn>{
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
};
The objective is to create an object of type GridColumn
that includes all the declared properties along with their default values.
The following approach does not achieve this:
private gridConf: GridColumn = GridColumn({
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
});
Using a constructor would require specifying all properties in order, which is not ideal.
The ultimate goal is to have the flexibility to define and utilize default and/or specified properties in any order like this:
private columns: Array<GridColumn> = [
<GridColumn>{
field: "test",
selectionMode: "single",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
header: "Test"
},
<GridColumn>{
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
}
];
The closest workaround found was removing the abstract keyword and adding a constructor where fields refer to itself:
public constructor(
fields?: GridColumn) {
if (fields) Object.assign(this, fields);
}