打字稿转换联合成交集

时间:2019-03-01 04:52:22

标签: typescript typescript-typings

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;

所以我期望得到一个十字路口而不是一个工会。 结果应该是所有产生的道具之间共有的值。 对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

can convert unions to intersections使用TS2.8及更高版本。在您的情况下,我可能会这样做:

type UnionToIntersection<U> =
  (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;

// use Lookup<T, K> instead of T[K] in cases where the compiler 
//  cannot verify that K is a key of T
type Lookup<T, K> = K extends keyof T ? T[K] : never;

type getKeyProp<T extends keyEnum> = Lookup<UnionToIntersection<keyPropObj[T]>, 'prop'>;

您想要的类型会根据需要落入

type getKeyPropResult1 = getKeyProp<keyEnum.thirdKey | keyEnum.secKey> // secPropEnum.
type getKeyPropResult2 = getKeyProp<keyEnum.thirdKey | keyEnum.firstKey> // firstPropEnum.
type getKeyPropResult3 = getKeyProp<keyEnum.secKey | keyEnum.firstKey> // never.

希望有帮助。祝你好运!

答案 1 :(得分:0)

好吧,您总是可以自己将类型相交:

type getKeyPropResult1 = getKeyProp<keyEnum.thirdKey> & getKeyProp<keyEnum.secKey>
type getKeyPropResult2 = getKeyProp<keyEnum.thirdKey> & getKeyProp<keyEnum.firstKey>
type getKeyPropResult3 = getKeyProp<keyEnum.secKey> & getKeyProp<keyEnum.firstKey>

一个丑陋的替代方案,最多可使用6个键:

type getKeyPropSingle<T extends keyEnum> = keyPropObj[T]['prop'];
type getKeyProp<
    T1 extends keyEnum | null = null,
    T2 extends keyEnum | null = null,
    T3 extends keyEnum | null = null,
    T4 extends keyEnum | null = null,
    T5 extends keyEnum | null = null,
    T6 extends keyEnum | null = null,
> = (T1 extends keyEnum ? getKeyPropSingle<T1> : unknown) &
    (T2 extends keyEnum ? getKeyPropSingle<T2> : unknown) &
    (T3 extends keyEnum ? getKeyPropSingle<T3> : unknown) &
    (T4 extends keyEnum ? getKeyPropSingle<T4> : unknown) &
    (T5 extends keyEnum ? getKeyPropSingle<T5> : unknown) &
    (T6 extends keyEnum ? getKeyPropSingle<T6> : unknown);

type second = getKeyProp<keyEnum.secKey>;
type getKeyPropResult1 = getKeyProp<keyEnum.thirdKey, keyEnum.secKey>
type getKeyPropResult2 = getKeyProp<keyEnum.thirdKey, keyEnum.firstKey>
type getKeyPropResult3 = getKeyProp<keyEnum.secKey, keyEnum.firstKey>