首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typescript重构

Typescript重构
EN

Stack Overflow用户
提问于 2021-11-25 20:10:27
回答 1查看 41关注 0票数 2

此代码运行正常。但对我来说看起来很奇怪。特别是这部分`城市常量= [].concat(...locationCities).includes('London');if( === true)

apiClient:

代码语言:javascript
复制
export interface apiRequest {
  locations: LocationOptions[];

}
interface FOLocations {
  locationId: number;
}

interface LocationResult {
  location: FOLocations[];
  address: Address;
}
interface LocationResult {
country:string;
city:string;
}
export const locationCheck = async (
  apiKey: string,
  payload: apiRequest
): Promise<LocationResult[]> => {
  let response = await axios.post<LocationResult[]>(
    `api...`,
    payload
  );
  return response.data;

runCode:

代码语言:javascript
复制
  const locationPayload : apiRequest = {
      locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
    };
    const locationResponse = await locationCheck(apiKey,locationPayload);
    const locationCities = locationResponse.map((location) => location.address.map(options => {return options.country}));
    console.log(locationCities);
    const city = [].concat(...locationCities).includes('London');
    console.log(city);
代码语言:javascript
复制
  if(city === true){
      console.log(`London location exist`);
    }else {
      throw new Error(
            "error"
      );
    }

输出:

代码语言:javascript
复制
true
London location exist
EN

回答 1

Stack Overflow用户

发布于 2021-11-25 20:52:50

我认为你可以删除这一行:

代码语言:javascript
复制
const city = [].concat(...locationCities).includes('London');

并替换为:

代码语言:javascript
复制
if(city === true){
  // ...

有了这个:

代码语言:javascript
复制
if(locationCities.includes('London')){
  // ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70116734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档