🎉

[TypeScript] 複数のKeyから引くMap

2021/09/12に公開
type UniqueId = string
type Obj = ...

const numberKeyMap = new Map<number, UniqueId>()
const stringKeyMap = new Map<string, UniqueId>()
const map = new Map<UniqueId, Obj>()

function getObjByNumberKey(key: number): Obj | undefined {
  const id = numberKeyMap.get(key)
  if (id == undefined) return
  return map.get(id)
}

function getObjByStringKey(key: string): Obj | undefined {
  const id = stringKeyMap.get(key)
  if (id == undefined) return
  return map.get(id)
}

Discussion