【发布时间】:2019-07-23 02:28:47
【问题描述】:
enum keyEnum {
firstKey = 1,
secKey = 2,
thirdKey = 3
};
enum firstPropEnum {
a = 'a',
b = 'b',
};
enum secPropEnum {
c = 'c',
d = 'd',
};
type firstAndSecPropEnum = firstPropEnum | secPropEnum;
type keyPropObj = {
[keyEnum.firstKey]: { prop: firstPropEnum },
[keyEnum.secKey]: { prop: secPropEnum },
[keyEnum.thirdKey]: { prop: firstAndSecPropEnum },
};
type getKeyProp<T extends keyEnum> = keyPropObj[T]['prop'];
type getKeyPropResult1 = getKeyProp<keyEnum.thirdKey | keyEnum.secKey> // Result secPropEnum | firstPropEnum
// Expected Result secPropEnum.
type getKeyPropResult2 = getKeyProp<keyEnum.thirdKey | keyEnum.firstKey> // Result firstPropEnum | secPropEnum
// Expected Result firstPropEnum.
type getKeyPropResult3 = getKeyProp<keyEnum.secKey | keyEnum.firstKey> // Result firstPropEnum | secPropEnum
// Expected Result never;
所以我期待得到一个交叉点而不是一个联合。 Result 应该是一个在所有结果 props 中通用的值。 任何对此的帮助将不胜感激。
【问题讨论】:
标签: typescript typescript-typings