【问题标题】:Use map object instead of params correctly正确使用地图对象而不是参数
【发布时间】:2022-01-21 15:50:24
【问题描述】:

我想使用地图对象而不是调用 checkOrder 3 次。在这种情况下如何通过 Map 而不是所有参数使用? :

Admin.ts

class Admin{
  async checkTest(
    local: string,
    suburb: string,
    international: string
  ): Promise<void> {
    await checkOrder(local, "Declined");
    await checkOrder(suburb, "Declined");
    await checkOrder(international, "Declined");
    return;
  }
}

Test.ts

test("Test", async (t) => {
  await Admin.checkTest(
    "Local Place",
    "Suburb Place",
    "International Place"
  );
  );
}

【问题讨论】:

  • 我不明白你想要什么。你能澄清一下吗?什么地图?
  • @Thomas 我想使用地图对象而不是调用 checkOrder 3 次。
  • Array 可以做同样的事情,并且在调用 checkOrder 时具有固定的顺序 那么为什么要映射呢?
  • @ABOS 我只是在寻找最好的方法。如果可能的话,你能推荐你的版本吗?

标签: javascript typescript function testing mapped-types


【解决方案1】:

@Thomas 我想使用地图对象而不是调用 checkOrder 3 次。

喜欢这样吗?

class Admin {
  async checkTest(map: Map<Place, Status>) {
    for (let [place, status] of map) {
      await checkOrder(place, status);
    }
  }
}

test("Test", async (t) => {
  await Admin.checkTest(
    new Map([
      ["Local Place", "Declined"],
      ["Suburb Place", "Declined"],
      ["International Place", "Declined"],
    ])
  );
});

【讨论】:

  • 谢谢,像这样。但是,我有一个错误:找不到名称“地点”和“状态”
  • @Puzzle234 我从你的问题中得到了 1:1。我认为这将是键和值的类型
  • @不明白,我需要添加额外的接口还是什么?
  • @Puzzle234 你真的写了“如何通过地图使用(Map)而不是所有参数”。你从哪里得到这些类型的?
  • 让我们重新开始,好吧,忘记。在我的情况下,如何使用地图对象而不是调用 checkOrder 3 次?
【解决方案2】:

那么,在我的情况下使用正确的地图吗?

class Admin {
  async checkTest(map: Map<string, string>) {
    for (let [place, status] of map) {
      await checkOrder(place, status);
    }
  }
}

test("Test", async (t) => {
  await Admin.checkTest(
    new Map([
      ["Local Place", "Declined"],
      ["Suburb Place", "Declined"],
      ["International Place", "Declined"],
    ])
  );
});

【讨论】:

  • 不要在回答中提问。这看起来也是从下面的答案复制而来的
猜你喜欢
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2017-06-25
相关资源
最近更新 更多