简体   繁体   English

类型 [] 打字稿上不存在属性

[英]property does not exist on type [] typescript

Hey I am trying to loop through an array and find a match to a value however I am having some typescript issues.嘿,我正在尝试遍历一个数组并找到一个值的匹配项,但是我遇到了一些打字稿问题。 When using the findIndex function to depict if there is a match in my array I am receiving the following error.当使用 findIndex 函数描述数组中是否存在匹配项时,我收到以下错误。

Property 'continents' does not exist on type 'Main[]' “Main[]”类型上不存在属性“大洲”

Not exactly sure what's going on here, I am guessing something is wrong with my mapping.不确定这里发生了什么,我猜我的映射有问题。

Interfaces:接口:

 interface Main {
  continents: Array<Continents>;
}

interface Continents {
  name: string;
  url: string;
  countries?: Array<Countries>;
}

interface Countries {
  name: string;
  url: string;
  states: string | null;
  featuredCities: Array<Cities>;
}

interface Cities {
  name: string;
  url: string;
}

State:状态:

  const [staticLocationHierarchy, setStaticLocationHierarchy] = useState<Array<Main>>([]);

Business Logic:商业逻辑:

const checkOne = staticLocationHierarchy?.continents.findIndex(
  continents => continents.url === test
);

I don't know exactly the value you are looking for however, you need to start by cleaning some variables, making your code clean will help typescript know what you want.我不确切知道您正在寻找的值,但是您需要先清理一些变量,使代码干净将有助于打字稿知道您想要什么。 I just removed and rename some variable I hope it'll help我刚刚删除并重命名了一些变量我希望它会有所帮助

 interface Continent {
      name: string;
      url: string;
      country?: Country[];
    }
    
    interface Country {
      name: string;
      url: string;
      states: string | null;
      featuredCity: City[];
    }
    
    interface City {
      name: string;
      url: string;
    }
    
    
    const [staticLocationHierarchy, setStaticLocationHierarchy] = useState<Continent[]>([]);
    
    const checkOne = staticLocationHierarchy.filter((continent, index) => {
        return continent.url = test
    })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM