typescript - 是否可以在 TypeScript 中的接口之间强制执行相同的动态键?
问题描述
给定一个MyInterface1
带有一组动态键的接口:
是否可以从中拉出钥匙MyInterface1
以用作钥匙MyInterface2
?
就像是:
export interface MyInterface1 {
[key: string]: string
}
export interface MyInterface2 {
[k in keyof MyInterface1]: string, // This line doesnt work :(
}
我在这里看到了一些这样的讨论:https ://github.com/Microsoft/TypeScript/issues/5683#issuecomment-376505064
解决方案
您需要声明类型别名,而不是接口:
export type MyInterface2 = {
[k in keyof MyInterface1]: string
};
推荐阅读
- vue.js - 解析错误:标识符“商店”已被声明。我在 main.js 上收到此错误,我不知道如何继续
- php - 在laravel中显示登录用户的记录
- c# - WPF获取绑定到observablecollection的选定组合框值
- excel - CopyPicture 有时会在选择一组形状后抛出错误
- html - 我想在列表中添加项目(vue.js)
- c++ - 为什么不调用成员变量的移动构造函数?
- angular - 我想用 ionic 5 制作能按时播放声音的应用程序
- html - CSS left:100% 超出设备屏幕尺寸
- php - 如何将值从视图发送到控制器?
- json - JMESPath 嵌套 AND 查询(单行)