🎉

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

1 min read
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

ログインするとコメントできます