【问题标题】:'shuterstock_init' only refers to a type, but is being used as a value here'shuterstock_init' 仅指一种类型,但在此处用作值
【发布时间】:2020-12-05 04:41:57
【问题描述】:

我有服务class imageService 与大多数键值对

export type servicesName = "unsplash" | "pexels" | "pixabay" | 'shutterstock';

export type Service = {
  [key in servicesName]?: string;

};

class ImageServices {
  services: Service;
  constructor(services: Service) {
    // creating key-value pair, key -> name of service, value api key (easy to access)
    this.services = { ...services };
  }
// code

现在,Shutterstock 实际上提供 cosumer_key 和 consumer_secret 而不仅仅是 api_key。我希望这反映在我的类型中,即如果 key 是 shutterstock,那么它将是带有 consumer_key 和 consume_secret 的对象,所以我做了这样的事情

interface shuterstock_init {
  consumer_id: string
  consumer_secret: string
}


export type Service = {
  [key in servicesName]?: string;
  shutterstock?: shuterstock_init
};

但这会引发以下错误

'}' expected.ts(1005)
Cannot find name 'shutterstock'.

'shuterstock_init' only refers to a type, but is being used as a value here

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    好的,我做到了,它成功了

    interface Service {
      unsplash?: string;
      pexels?: string;
      pixabay?: string;
      shutterstock?: shuterstock_init
    };
    

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 2020-04-28
      • 2021-08-23
      • 2018-03-31
      • 1970-01-01
      • 2023-04-03
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多