Search code examples
typescriptinterface

Is it possible to standardize types in an interface?


export interface BookProps {
  link: string;
  title: string;
  image: string;
  author: string;
  discount: string;
  publisher: string;
  description: string;
}

Is there a way to express that all elements in the interface have a string type in a concise manner?


Solution

  • You could use index properties to achieve a general interface definition.

    interface Book {
        [prop: string]: string;
    }
    
    const nineteenEightyFour: Book = {
        link: "https://...",
        title: "nineteenEightyFour",
        .... 
    }
    

    Ts should be happy enough with this.