Skip to content

Typescript: How do I define an interface for an object type’s key value pair

I have an object called externalObject that has various key:value pairs.

I also have a typescript interface that is defined as the following:

interface TestObject{
   externalObject?: {}

My question is how do I further set the type for the externalObject’s key as string and the values that are passed inside externalObject as string or number?

Note: we do not always know the key:value pairs. They vary each time.


You can set any number of key/types on an interface in a similar way to assigning an object. If you don’t know the property names ahead of time, you can use a dynamic key:

export interface ITestObject {
  externalObject: {
    [key: string]: string | number;

Alternatively, you can set the property as unknown and cast it to the proper type.