首页 > 解决方案 > 'shuterstock_init' 仅指一种类型,但在此处用作值

问题描述

我有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

解决方案


好的,我做到了,它奏效了

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

推荐阅读