1

I would like to do something like this

interface Foo {
  firstException: number;
  secondException: string[];

  [key: string]: string; // doesn't do what I want
}

But the last line causes type errors

Property 'firstException' of type 'number' is not assignable to 'string' index type 'string'.

I have to do [key: string]: any; or [key: string]: unknown;. I could also do [key: string]: string | number | string[]; but then I'll have to check the type any time I access a property.

Is there a way to say that all keys that are not explicitly defined will hold string values?

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • 1
    No, unfortunately there is no specific type in TS that works this way; you might want to upvote [ms/TS#17867](https://github.com/microsoft/TypeScript/issues/17867) which asks for such a feature. See the linked question for more info and workarounds. – jcalz Jan 07 '23 at 17:09

0 Answers0