Home > OS >  TypeScript - Object character map - Element implicitly has an 'any' type because expressio
TypeScript - Object character map - Element implicitly has an 'any' type because expressio

Time:01-03

I am new to TypeScript. I looked at similar questions and this one seemed different enough to ask on its own to me because it is a character map. I am confused by the syntax and several blog posts I saw about this error.

The following code has been partially converted from JavaScript to TypeScript. It is supposed to compare two strings to see if they are anagrams, a common code challenge. When I try and compile I get the following errors:

src/variations.ts:24:13 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

24             map[char]  ;
               ~~~~~~~~~
src/variations.ts:26:13 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

26             map[char] = 1;
               ~~~~~~~~~
src/variations.ts:38:9 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

38     if (str1map[char] !== str2map[char]) {
           ~~~~~~~~~~~~~
src/variations.ts:38:27 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

38     if (str1map[char] !== str2map[char]) {
                             ~~~~~~~~~~~~~

Code: src/variations.ts

/** 
 * @param {string} string 1, 
 * @param {string} string 2,
 * @return {string} result of anagram comparison
 */
function isAnagram(str1: string, str2: string) {

  // Earliest correct exit opportunity: identical strings are anagram
  if(str1 === str2) {
    return "ANAGRAM";
  }
  // Are the strings the same length? Exit if not;
  if (str1.length !== str2.length) {
    console.log('Strings not same length');
    return "NOT ANAGRAM!";
  }

  // Function to create character map from a string
  function createCharacterMap(text: string) {
    let map = {};
    for (let char of text) {
        char = char.toLowerCase();
        if (map.hasOwnProperty(char)) {
            map[char]  ;
        } else {
            map[char] = 1;
        }
    }
    return map;
  }
  // Create Character Maps
  const str1map = createCharacterMap(str1);
  const str2map = createCharacterMap(str2);

  // Is character in str1 in str2?
  // NB: Issue of characters existing in str2 but not str1 addressed by string length check
  for (let char in str1map) {
    if (str1map[char] !== str2map[char]) {
      return "NOT ANAGRAM!";
    }
  }
 
  return "ANAGRAM!";
}

module.exports = { isAnagram };

I would appreciate help. I simply dont get how to create this index signature given the character map configuration

CodePudding user response:

It's because your map let map = {}; has neither a type nor a default field, so typescript sees this as an implicit any type.

To resolve this, you can use let map: any = {}; to have an explicit any type,

or let map = {[char: string]: number} = {}; for true typing

Edit: if you didn't know, the syntax to define an object with a format of key/value is {[KeyName: KeyType]: ValueType, Otherkey: itstype, ...}

  • Related