typescript - 打字稿动态键名
问题描述
我只想拥有一个具有动态属性的对象,例如( get , post , put )
type methodNames = "get" | "post" | "put";
interface toFetch {
target:string,
from: string
}
type CommonApiQuery = Record<methodNames, toFetch[]>
我希望 CommonAPIQuery 允许对象如下所示:
const lookup : CommonApiQuery = {
get: [{target:"students",from:"id"}]
}
但它的预期属性“post”和“put”也是。我只想在 methodNames 中至少有一个已定义的属性,但是当没有定义 methodNames 的属性时应该抛出错误。如何做到这一点?
解决方案
推荐阅读
- javascript - MVP 设计模式中的业务逻辑在哪里?
- tinymce - 在 Connections 6 的 TinyMCE 编辑器中为图像添加边框的 GUI 设置
- r - RODBC - 查询向量长于 1
- excel - 在标题后将行添加到表格顶部
- azure-sql-database - 如何从 Azure SQL Server 设置自动 CSV 导出作业?
- sql-server - 弹性搜索 SQL Server 同步
- c# - 使用 .net core 3.1 中的 System.Text.Json 从文件中加载 json 后,从 json 中删除空格
- ios - 无法将数据传递给 ContainerView
- postgresql - Camunda:如何配置外部 Postgres 数据库?
- php - Laravel 视图 foreach 有两个属性