TypeScript: how to type a POJO


This post goes over how to type a Plain Old JavaScript Object (POJO) in TypeScript:

See playground.

Interface

To type a POJO using an interface:

interface IObject {
  [key: string]: any;
}

Usage example:

const object1: IObject = {
  foo: 'bar',
};

Type Alias

To type a POJO using a type alias:

type ObjectType = {
  [key: string]: any;
};

Usage example:

const object2: ObjectType = {
  foo: 'bar',
};

Record

To type a POJO using a record:

Record<string, any>;

Usage example:

const object3: Record<string, any> = {
  foo: 'bar',
};


Please support this site and join our Discord!