jumbopap
jumbopap

Reputation: 4137

Deriving TypeScript interface from object literal

Does TypeScript have the ability to infer an interface from an object literal? Like the following:

export const theme = {
  primaryColor: "#e9e9eb",
  secondaryColor: "blue"
};

The interface would be the following, which (AFAICT), TypeScript compels me to create a separate interface for, but I would like to automatically derive.

export interface IThemeInterface {
  primaryColor: string;
  secondaryColor: string;
}

Upvotes: 2

Views: 97

Answers (1)

jumbopap
jumbopap

Reputation: 4137

type IThemeInterface = typeof theme; does the correct thing here.

Upvotes: 2

Related Questions