I have some Interfaces which look like this:
export enum SortValueType {
String = 'string',
Number = 'number',
Date = 'date',
}
export interface SortConfig {
key: string;
direction: SortDirection;
type: SortValueType;
options?: {
sortBy?: 'year' | 'day';
};
}
I would like to make it possible to extend this so that the possible types of options.sortBy should be dependent on the type. I am creating iteratees depending on type so it should not be possible to create an Object where type is string and options.sortBy has a value of year.
This is the getIteratee function:
private getIteratee(config: SortConfig) {
if (config.type === SortValueType.String) {
return item => _lowerCase(item[config.key]);
}
if (config.type === SortValueType.Date) {
const sortBy = _get(config, 'options.sortBy') as 'year' | 'day';
if (!!sortBy && sortBy === 'year') {
return item =>
!!item[config.key]
? new Date(item[config.key]).getFullYear()
: undefined;
}
if (!!sortBy && sortBy === 'day') {
return item =>
!!item[config.key]
? new Date(item[config.key].toJSON().split('T')[0])
: undefined;
}
}
return config.key;
}
I am also open to more generic solutions