简体   繁体   English

元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 '{}' - React Anagram

[英]Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram

I've been trying to extend my React capabilities, so trying to move a project over from Javascript to Typescript, but I'm getting some errors that I can't seem to fix.我一直在尝试扩展我的 React 功能,因此尝试将项目从 Javascript 转移到 Typescript,但我遇到了一些似乎无法修复的错误。

I've done a lot of reading and searching, but can't seem to figure out what I'm doing wrong.我已经做了很多阅读和搜索,但似乎无法弄清楚我做错了什么。

The other components work, as well as the main App.其他组件以及主应用程序都可以工作。 It's just this component.这只是这个组件。

// Imports
interface Props { }

interface State { }

class Anagram extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
  }

  alphabetize(word: any) {
    if (word) {
      word = word.split('');
      word = word.sort();
      word = word.join('');
      return word;
    }
    return;
  }

  getGroupedAnagrams(words: string[]) {
    const anagrams = {};
    words.forEach((word) => {
      const sortedWord = this.alphabetize(word);
      if (anagrams[sortedWord]) {
        return anagrams[sortedWord].push(word);
      }
      anagrams[sortedWord] = [word];
    });
    return anagrams;
  }

  render() {
    const words = ['map', 'art', 'how', 'rat', 'tar', 'who', 'pam', 'shoop'];
    const groupedAnagrams = this.getGroupedAnagrams(words);

    for (const sortedWord in groupedAnagrams) {
      console.log(groupedAnagrams[sortedWord].toString());
    }
    return (
      // Body elements
    );
  }
}

export default Anagram;

So, the first function takes in a string and splits it apart and creates an alphabetized string.因此,第一个函数接受一个字符串并将其拆分并创建一个按字母顺序排列的字符串。 Then, the next function takes in an array of strings, then for each word in the array it runs the word through the first function, then checks if the anagrams object already contains that "key".然后,下一个函数接受一个字符串数组,然后对于数组中的每个单词,它通过第一个函数运行单词,然后检查 anagrams 对象是否已经包含该“键”。 And if it does, then it pushes the original word into the array of that alphabetized word.如果是,那么它将原始单词推入该字母顺序单词的数组中。 If it doesn't contain that word, then it adds that "key".如果它不包含该词,则它会添加该“键”。

My problem is that all the areas that have [sortedWord] return the error:我的问题是所有具有 [sortedWord] 的区域都返回错误:

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}'.

What have I done wrong?我做错了什么? The project won't compile correctly until this error is fixed.在修复此错误之前,项目将无法正确编译。

'word' is of the type 'any'. “word”属于“any”类型。 When you use the return from alphabetize() and set it as an index for traversing anagrams.当您使用来自字母表()的返回并将其设置为遍历字谜的索引时。

anagrams[sortedWord]

Change the code to this, and it will work:将代码更改为此,它将起作用:

alphabetize(word: string) {
    if (word) {
      let words = word.split('');
      words = words.sort();
      word = words.join('');
      return word;
    }
    return;
}

getGroupedAnagrams(words: string[]) {
    const anagrams = {} as any;
    words.forEach((word) => {
      const sortedWord = this.alphabetize(word);
      if (sortedWord) {
        if( anagrams[sortedWord]){
          return anagrams[sortedWord].push(word); 
        }
        anagrams[sortedWord] = [word];
      } 
    });
    return anagrams;
  }

Best doc for typescript: https://www.typescriptlang.org/docs/home.html打字稿的最佳文档: https : //www.typescriptlang.org/docs/home.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React typescript - 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型 - React typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 React Typescript 中索引类型 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript 带有 React &gt; Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引“Item”类型 - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item' 元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型“{服务:字符串; } - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ service: string; } 元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于使用 createStyles 来索引类型 - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type with createStyles 元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM