是否可以标准化接口中的类型?

问题描述 投票:0回答:1
export interface BookProps {
  link: string;
  title: string;
  image: string;
  author: string;
  discount: string;
  publisher: string;
  description: string;
}

有没有办法简洁的表达界面中所有元素都是字符串类型?

typescript interface
1个回答
0
投票

您可以使用索引属性来实现通用接口定义。

interface Book {
    [prop: string]: string;
}

const nineteenEightyFour: Book = {
    link: "https://...",
    title: "nineteenEightyFour",
    .... 
}

Ts应该对此感到足够高兴。

© www.soinside.com 2019 - 2024. All rights reserved.