【发布时间】:2019-07-29 11:34:44
【问题描述】:
在一个文件中我有这样的内容:
export const _all = {
a: '',
b: '',
c: '',
d: '',
e: '',
f: '',
}
type AllKeysType = typeof _all;
export type AllKey = keyof AllKeysType;
在另一个文件中我有这样的东西:
export const _keep = {
a: '',
b: '',
d: '',
e: '',
}
type KeepKeysType = typeof _keep;
export type KeepKey = keyof KeepKeysType;
export const _ignore = {
c: '',
f: '',
}
type IgnoreKeysType = typeof _ignore;
export type IgnoreKey = keyof IgnoreKeysType;
如何使用 Typescript 断言 _all 中定义的键始终等于 _keep 和 _ignore 的并集。换句话说,AllKey 应该始终等于KeepKey | IgnoreKey.
如果开发人员通过添加新值(例如 z)更新 _all 但忘记将 z 添加到 _keep 或 _ignore 中,我希望 Typescript 编译器给我一个错误。
【问题讨论】:
标签: typescript types assert typeof keyof