N
N
newaitix2020-03-13 20:53:10
typescript
newaitix, 2020-03-13 20:53:10

How to fix Element implicitly has an 'any' error?

How to fix the error?

function serialize(arg) {
  var res = '';
  var replaceNamePop = {
    modules: 'module',
    categories: 'category',
    searchFieldsList: 'searchFieldList',
  };
  Object.keys(arg).forEach(function(key) {
    if(Array.isArray(this[key])) {
      for(var j = 0; j < this[key].length; j++) {
        if(typeof replaceNamePop[key] != 'undefined')
          res += '&' + replaceNamePop[key] + '=' + this[key][j];
        else
          res += '&' + key + '=' + this[key][j];
      }
    }else{
      if(typeof replaceNamePop[key] != 'undefined')
        res += '&' + replaceNamePop[key] + '=' + this[key];
      else
        res += '&' + key + '=' + this[key];
    }
  }, arg);
  return res;
}
serialize({
  "modules": [20],
  "categories" : [20],
  'keyword': "keyword",
  'time':"time",
  'catRelation':"cat",
  'sortField':"sort",
  'sortDirection':"ASC",
  'searchFieldsList':"list",
});
serialize({
  "modules": [20, 25],
  "categories" : [20, 58],
  'keyword': "keyword",
  'time':"time",
  'catRelation':"cat",
  'sortField':"sort",
  'sortDirection':"ASC",
  'searchFieldsList':"list",
});
serialize({
  "modules": [20, 25],
  'keyword': "keyword",
});


5e6bc88330750483017441.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-03-13
@Aetae

interface Dictionary<T> {
  [key: string]: T
}

function serialize<T extends Dictionary<string | number[]>> (arg: T) {
  var res = '';

  var replaceNamePop: Dictionary<string> = {
    modules: 'module',
    categories: 'category',
    searchFieldsList: 'searchFieldList'
  };

  Object.keys(arg).forEach(function (this: T, key) {
    if (Array.isArray(this[key])) {
      for (var j = 0; j < this[key].length; j++) {
        if (typeof replaceNamePop[key] != 'undefined')
          res += '&' + replaceNamePop[key] + '=' + this[key][j];
        else
          res += '&' + key + '=' + this[key][j];
      }
    } else {
      if (typeof replaceNamePop[key] != 'undefined')
        res += '&' + replaceNamePop[key] + '=' + this[key];
      else
        res += '&' + key + '=' + this[key];
    }
  }, arg);
  return res;
}

serialize({
  'modules': [20],
  'categories': [20],
  'keyword': 'keyword',
  'time': 'time',
  'catRelation': 'cat',
  'sortField': 'sort',
  'sortDirection': 'ASC',
  'searchFieldsList': 'list'
});
serialize({
  'modules': [20, 25],
  'categories': [20, 58],
  'keyword': 'keyword',
  'time': 'time',
  'catRelation': 'cat',
  'sortField': 'sort',
  'sortDirection': 'ASC',
  'searchFieldsList': 'list'
});
serialize({
  'modules': [20, 25],
  'keyword': 'keyword'
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question