typescript - '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
有人可以帮我解决这个问题吗?
解决方案
好的,我做到了,它奏效了
interface Service {
unsplash?: string;
pexels?: string;
pixabay?: string;
shutterstock?: shuterstock_init
};
推荐阅读
- visual-studio - 有没有办法在 vsTemplates 中添加条件?或根据用户选择生成输出项目的方法
- vue.js - 不能在子组件中使用父组件的mixins功能吗?
- html - 如何使用 Bootstrap 进行多级下拉菜单?
- android - 我可以尝试创建登录页面,但在状态电子邮件上出现错误
- r - 在 R 中生成 pca 图
- python - Selenium 通过 pip3 安装,但使用 import selenium 时找不到名为 selenium 的模块
- javascript - 如何获取url参数并将其设置为文本框onload的默认值
- c# - 如何在asp.net Core中的每次批量操作后发送进度成功消息
- javascript - javascript while循环问题,break不会停止循环
- java - 如何将 Java json 对象发送到 R 函数?