import type {User, CustomFilter} from '../app'
const sort = (data: User[], filters: CustomFilter): User[] => {
if(filters.length === 0) return data
return data.sort((a,b)=> {
for(const filter of filters){
const {key, value} = filter;
if (value === 'ASC') {
if (a[key] < b[key]) return -1;
if (a[key] > b[key]) return 1;
} else if (value === 'DESC') {
if (a[key] > b[key]) return -1;
if (a[key] < b[key]) return 1;
}
}
return 0;
});
}
const settings: ['ASC', 'DESC'] = ['ASC', 'DESC']
sort.settings = settings;
console.log(sort.settings); // work
type CustomSortFunction = (data: User[], filters: CustomFilter) => User[];
type Sorts = {
[SortFunctionName: string]: CustomSortFunction;
}
const sorts: Sorts = { sort };
console.log(sorts.sort.settings); // Don't see property "settings"
console.log(sorts.settings); // Property "settings" is available. I don't really understand why
Why am I getting an error at the line console.log(sorts.sort.settings); . But there is no error in the line console.log(sorts.settings);.