javascript - Typescript:使用枚举的自定义接口类型
问题描述
我正在使用我的 API 端点处理类别,因此我enum
为它创建了这个:
export enum categories {
DESIGN = 'design',
ART = 'art',
WRITING = 'writing',
PHOTOGRAPHY = 'photography',
MUSIC = 'music',
DIGITAL = 'digital',
MEDIA = 'media'
}
现在我想用它来为category
我的数据库中的属性设置一个接口。
interface
为了那个原因 :
interface ProjectAttrs {
user: string;
title: string;
description: string;
category: // <== I want it to be either of those values defined above
}
如何将类别接口类型指定为枚举中定义的任一值?
解决方案
只需像这样输入枚举名称categories
:
interface ProjectAttrs {
user: string;
title: string;
description: string;
category: categories;
}
这是一个工作演示
推荐阅读
- maven - 未找到 Maven 阴影输出
- java - Java Map with TimeToLive 与每个键/值对相关联
- python - PyQt 使 QFrame 出现在另一个小部件的顶部
- amazon-s3 - 修改 S3 的 CloudFront 源请求
- java - Jenkinsfile 路径位置“@2 转换为 %402”并且构建失败
- html - 如何防止在固定导航栏中查看跨度符号
- winapi - 如何获取未记录的 DShellFolderViewEvents 的方法签名
- java - 尝试光栅化 SVG 文件时出现 Batik 1.14 错误
- javascript - 当标记 onClick 触发状态更新时,黑色方块出现在第一个标记周围
- r - 按扇区计算 R 中的标准差