0

i have the following typescript code

type MapOfErrors = Map<string, Error[]>
interface GatheredErrors {
  'dev': MapOfErrors
  'prod': MapOfErrors
  [key: string]: MapOfErrors
}

const errors: GatheredErrors = {
  dev: new Map<string, Array<Error>>(),
  prod: new Map<string, Array<Error>>()

}

 errors[ctx.env]['something'] = []

where ctx is of Type Context

interface Context {
  token: string
  env: "dev" | "prod"
}

I get the following error

src/index.ts:136:5 - error TS7017: Element implicitly has an 'any' type because type 'Map<string, Error[]>' has no index signature.

136     errors[ctx.env]['something'] = []

I'm not sure on how to add the index signature to the Map type

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

1 Answers1

2

Maps don't support the index syntax like you expected they would. They are accessed using methods such as .has(key), .get(key), and .set(key, value):

errors[ctx.env].set('something', [])
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268